timeFuns.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. defined('IN_WEB') or die('Include Error!');
  3. /**
  4. * Created 时间操作函数库
  5. * User: wsc
  6. * Date: 2018/12/12
  7. * Time: 9:45
  8. */
  9. class timeFuns
  10. {
  11. /**
  12. * 当前毫秒数
  13. *
  14. */
  15. public static function nowMsec(){
  16. //$msec当前毫秒 $sec当前秒时间戳
  17. list($usec, $sec) = explode(" ", microtime());
  18. $msec=round($usec*1000);
  19. $time = $sec*1000 + $msec;
  20. return $time;
  21. }
  22. /**
  23. * 返回今日开始和结束的时间戳
  24. *
  25. * @return array
  26. */
  27. public static function today()
  28. {
  29. return [
  30. mktime(0, 0, 0, date('m'), date('d'), date('Y')),
  31. mktime(23, 59, 59, date('m'), date('d'), date('Y'))
  32. ];
  33. }
  34. /**
  35. * 返回昨日开始和结束的时间戳
  36. *
  37. * @return array
  38. */
  39. public static function yesterday()
  40. {
  41. $yesterday = date('d') - 1;
  42. return [
  43. mktime(0, 0, 0, date('m'), $yesterday, date('Y')),
  44. mktime(23, 59, 59, date('m'), $yesterday, date('Y'))
  45. ];
  46. }
  47. /**
  48. * 返回本周开始和结束的时间戳
  49. *
  50. * @return array
  51. */
  52. public static function week()
  53. {
  54. $timestamp = time();
  55. return [
  56. strtotime(date('Y-m-d', strtotime("this week Monday", $timestamp))),
  57. strtotime(date('Y-m-d', strtotime("this week Sunday", $timestamp))) + 24 * 3600 - 1
  58. ];
  59. }
  60. /**
  61. * 返回上周开始和结束的时间戳
  62. *
  63. * @return array
  64. */
  65. public static function lastWeek()
  66. {
  67. $timestamp = time();
  68. return [
  69. strtotime(date('Y-m-d', strtotime("last week Monday", $timestamp))),
  70. strtotime(date('Y-m-d', strtotime("last week Sunday", $timestamp))) + 24 * 3600 - 1
  71. ];
  72. }
  73. /**
  74. * 返回本月开始和结束的时间戳
  75. *
  76. * @return array
  77. */
  78. public static function month($everyDay = false)
  79. {
  80. return [
  81. mktime(0, 0, 0, date('m'), 1, date('Y')),
  82. mktime(23, 59, 59, date('m'), date('t'), date('Y'))
  83. ];
  84. }
  85. /**
  86. * 返回上个月开始和结束的时间戳
  87. *
  88. * @return array
  89. */
  90. public static function lastMonth()
  91. {
  92. $begin = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
  93. $end = mktime(23, 59, 59, date('m') - 1, date('t', $begin), date('Y'));
  94. return [$begin, $end];
  95. }
  96. /**
  97. * 返回今年开始和结束的时间戳
  98. *
  99. * @return array
  100. */
  101. public static function year()
  102. {
  103. return [
  104. mktime(0, 0, 0, 1, 1, date('Y')),
  105. mktime(23, 59, 59, 12, 31, date('Y'))
  106. ];
  107. }
  108. /**
  109. * 返回去年开始和结束的时间戳
  110. *
  111. * @return array
  112. */
  113. public static function lastYear()
  114. {
  115. $year = date('Y') - 1;
  116. return [
  117. mktime(0, 0, 0, 1, 1, $year),
  118. mktime(23, 59, 59, 12, 31, $year)
  119. ];
  120. }
  121. public static function dayOf()
  122. {
  123. }
  124. /**
  125. * 获取几天前零点到现在/昨日结束的时间戳
  126. *
  127. * @param int $day 天数
  128. * @param bool $now 返回现在或者昨天结束时间戳
  129. * @return array
  130. */
  131. public static function dayToNow($day = 1, $now = true)
  132. {
  133. $end = time();
  134. if (!$now) {
  135. list($foo, $end) = self::yesterday();
  136. }
  137. return [
  138. mktime(0, 0, 0, date('m'), date('d') - $day, date('Y')),
  139. $end
  140. ];
  141. }
  142. /**
  143. * 返回几天前的时间戳
  144. *
  145. * @param int $day
  146. * @return int
  147. */
  148. public static function daysAgo($day = 1)
  149. {
  150. $nowTime = time();
  151. return $nowTime - self::daysToSecond($day);
  152. }
  153. /**
  154. * 返回几天后的时间戳
  155. *
  156. * @param int $day
  157. * @return int
  158. */
  159. public static function daysAfter($day = 1)
  160. {
  161. $nowTime = time();
  162. return $nowTime + self::daysToSecond($day);
  163. }
  164. /**
  165. * 天数转换成秒数
  166. *
  167. * @param int $day
  168. * @return int
  169. */
  170. public static function daysToSecond($day = 1)
  171. {
  172. return $day * 86400;
  173. }
  174. /**
  175. * 周数转换成秒数
  176. *
  177. * @param int $week
  178. * @return int
  179. */
  180. public static function weekToSecond($week = 1)
  181. {
  182. return self::daysToSecond() * 7 * $week;
  183. }
  184. /**
  185. * 毫秒转分秒毫秒
  186. * @param $msec
  187. * @return string
  188. */
  189. public static function msecToMinutes($msec){
  190. $i = floor(floor($msec/1000)/60);
  191. $s = floor(($msec-$i*60*1000)/1000);
  192. $ms = round(($msec%1000)/10);
  193. $arr = ['i'=>$i,'s'=>$s,'ms'=>$ms];
  194. // $str = $i.'`'.$s.'``'.$ms.'```';
  195. return $arr;
  196. }
  197. private static function startTimeToEndTime()
  198. {
  199. }
  200. /**
  201. * 判断周末
  202. * @param $str
  203. * @return bool
  204. */
  205. public static function isWeek($str){
  206. if((date('w',strtotime($str))==6) || (date('w',strtotime($str)) == 0)){
  207. return true;
  208. }else{
  209. return false;
  210. }
  211. }
  212. /**
  213. * 时间遍历,返回范围内时间
  214. * @param $date1
  215. * @param $date2
  216. * @param $t
  217. * @return array
  218. */
  219. public static function date_range($date1,$date2,$t='Ymd'){
  220. $timestamp1=strtotime($date1);
  221. $timestamp2=strtotime($date2);
  222. $days=($timestamp2-$timestamp1)/86400+1;
  223. $date=[];
  224. for($i=0;$i<$days;$i++){
  225. $date[]=date($t,$timestamp1+(86400*$i));
  226. }
  227. return $date;
  228. }
  229. /**
  230. * 今天过去分钟数
  231. * @param $time
  232. * @return false|float|int|string
  233. * Created by: Owen
  234. * Created on: 2020/4/24 19:50
  235. */
  236. public static function todaySecond($time = null){
  237. if($time == null){
  238. $time = time();
  239. }
  240. $hour = date('G', $time) * 60;
  241. return $hour + date('i', $time);
  242. }
  243. }