TimeService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\service;
  20. class TimeService
  21. {
  22. /**
  23. * 返回今日开始和结束的时间戳
  24. *
  25. * @return array
  26. */
  27. public static function today(){
  28. list($y, $m, $d) = explode('-', date('Y-m-d'));
  29. return [
  30. mktime(0, 0, 0, $m, $d, $y),
  31. mktime(23, 59, 59, $m, $d, $y),
  32. ];
  33. }
  34. /**
  35. * 返回昨日开始和结束的时间戳
  36. *
  37. * @return array
  38. */
  39. public static function yesterday(){
  40. $yesterday = date('d') - 1;
  41. return [
  42. mktime(0, 0, 0, date('m'), $yesterday, date('Y')),
  43. mktime(23, 59, 59, date('m'), $yesterday, date('Y')),
  44. ];
  45. }
  46. /**
  47. * 返回本周开始和结束的时间戳
  48. *
  49. * @return array
  50. */
  51. public static function week(){
  52. list($y, $m, $d, $w) = explode('-', date('Y-m-d-w'));
  53. if($w == 0) $w = 7; //修正周日的问题
  54. return [
  55. mktime(0, 0, 0, $m, $d - $w + 1, $y), mktime(23, 59, 59, $m, $d - $w + 7, $y),
  56. ];
  57. }
  58. /**
  59. * 返回上周开始和结束的时间戳
  60. *
  61. * @return array
  62. */
  63. public static function lastWeek(){
  64. $timestamp = time();
  65. return [
  66. strtotime(date('Y-m-d', strtotime("last week Monday", $timestamp))),
  67. strtotime(date('Y-m-d', strtotime("last week Sunday", $timestamp))) + 24 * 3600 - 1,
  68. ];
  69. }
  70. /**
  71. * 返回本月开始和结束的时间戳
  72. *
  73. * @return array
  74. */
  75. public static function month(){
  76. list($y, $m, $t) = explode('-', date('Y-m-t'));
  77. return [
  78. mktime(0, 0, 0, $m, 1, $y),
  79. mktime(23, 59, 59, $m, $t, $y),
  80. ];
  81. }
  82. /**
  83. * 返回上个月开始和结束的时间戳
  84. *
  85. * @return array
  86. */
  87. public static function lastMonth(){
  88. $y = date('Y');
  89. $m = date('m');
  90. $begin = mktime(0, 0, 0, $m - 1, 1, $y);
  91. $end = mktime(23, 59, 59, $m - 1, date('t', $begin), $y);
  92. return [$begin, $end];
  93. }
  94. /**
  95. * 返回今年开始和结束的时间戳
  96. *
  97. * @return array
  98. */
  99. public static function year(){
  100. $y = date('Y');
  101. return [
  102. mktime(0, 0, 0, 1, 1, $y),
  103. mktime(23, 59, 59, 12, 31, $y),
  104. ];
  105. }
  106. /**
  107. * 返回去年开始和结束的时间戳
  108. *
  109. * @return array
  110. */
  111. public static function lastYear(){
  112. $year = date('Y') - 1;
  113. return [
  114. mktime(0, 0, 0, 1, 1, $year),
  115. mktime(23, 59, 59, 12, 31, $year),
  116. ];
  117. }
  118. /**
  119. * 获取几天前零点到现在/昨日结束的时间戳
  120. *
  121. * @param int $day 天数
  122. * @param bool $now 返回现在或者昨天结束时间戳
  123. * @return array
  124. */
  125. public static function dayToNow($day = 1, $now = true){
  126. $end = time();
  127. if(!$now){
  128. list($foo, $end) = self::yesterday();
  129. }
  130. return [
  131. mktime(0, 0, 0, date('m'), date('d') - $day, date('Y')),
  132. $end,
  133. ];
  134. }
  135. /**
  136. * 返回几天前的时间戳
  137. *
  138. * @param int $day
  139. * @return int
  140. */
  141. public static function daysAgo($day = 1){
  142. $nowTime = time();
  143. return $nowTime - self::daysToSecond($day);
  144. }
  145. /**
  146. * 返回几天后的时间戳
  147. *
  148. * @param int $day
  149. * @return int
  150. */
  151. public static function daysAfter($day = 1){
  152. $nowTime = time();
  153. return $nowTime + self::daysToSecond($day);
  154. }
  155. /**
  156. * 天数转换成秒数
  157. *
  158. * @param int $day
  159. * @return int
  160. */
  161. public static function daysToSecond($day = 1){
  162. return $day * 86400;
  163. }
  164. /**
  165. * 周数转换成秒数
  166. *
  167. * @param int $week
  168. * @return int
  169. */
  170. public static function weekToSecond($week = 1){
  171. return self::daysToSecond() * 7 * $week;
  172. }
  173. /**
  174. * 获取毫秒级别的时间戳
  175. */
  176. public static function getMillisecond(){
  177. $time = explode(" ", microtime());
  178. $time = $time[1].($time[0] * 1000);
  179. $time2 = explode(".", $time);
  180. $time = $time2[0];
  181. return $time;
  182. }
  183. }