Time.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 刘志淳 <chun@engineer.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\helper;
  12. class Time
  13. {
  14. /**
  15. * 返回今日开始和结束的时间戳
  16. *
  17. * @return array
  18. */
  19. public static function today()
  20. {
  21. list($y, $m, $d) = explode('-', date('Y-m-d'));
  22. return [
  23. mktime(0, 0, 0, $m, $d, $y),
  24. mktime(23, 59, 59, $m, $d, $y)
  25. ];
  26. }
  27. /**
  28. * 返回昨日开始和结束的时间戳
  29. *
  30. * @return array
  31. */
  32. public static function yesterday()
  33. {
  34. $yesterday = date('d') - 1;
  35. return [
  36. mktime(0, 0, 0, date('m'), $yesterday, date('Y')),
  37. mktime(23, 59, 59, date('m'), $yesterday, date('Y'))
  38. ];
  39. }
  40. /**
  41. * 返回本周开始和结束的时间戳
  42. *
  43. * @return array
  44. */
  45. public static function week()
  46. {
  47. list($y, $m, $d, $w) = explode('-', date('Y-m-d-w'));
  48. if($w == 0) $w = 7; //修正周日的问题
  49. return [
  50. mktime(0, 0, 0, $m, $d - $w + 1, $y), mktime(23, 59, 59, $m, $d - $w + 7, $y)
  51. ];
  52. }
  53. /**
  54. * 返回上周开始和结束的时间戳
  55. *
  56. * @return array
  57. */
  58. public static function lastWeek()
  59. {
  60. $timestamp = time();
  61. return [
  62. strtotime(date('Y-m-d', strtotime("last week Monday", $timestamp))),
  63. strtotime(date('Y-m-d', strtotime("last week Sunday", $timestamp))) + 24 * 3600 - 1
  64. ];
  65. }
  66. /**
  67. * 返回本月开始和结束的时间戳
  68. *
  69. * @return array
  70. */
  71. public static function month($everyDay = false)
  72. {
  73. list($y, $m, $t) = explode('-', date('Y-m-t'));
  74. return [
  75. mktime(0, 0, 0, $m, 1, $y),
  76. mktime(23, 59, 59, $m, $t, $y)
  77. ];
  78. }
  79. /**
  80. * 返回上个月开始和结束的时间戳
  81. *
  82. * @return array
  83. */
  84. public static function lastMonth()
  85. {
  86. $y = date('Y');
  87. $m = date('m');
  88. $begin = mktime(0, 0, 0, $m - 1, 1, $y);
  89. $end = mktime(23, 59, 59, $m - 1, date('t', $begin), $y);
  90. return [$begin, $end];
  91. }
  92. /**
  93. * 返回今年开始和结束的时间戳
  94. *
  95. * @return array
  96. */
  97. public static function year()
  98. {
  99. $y = date('Y');
  100. return [
  101. mktime(0, 0, 0, 1, 1, $y),
  102. mktime(23, 59, 59, 12, 31, $y)
  103. ];
  104. }
  105. /**
  106. * 返回去年开始和结束的时间戳
  107. *
  108. * @return array
  109. */
  110. public static function lastYear()
  111. {
  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. public static function dayOf()
  119. {
  120. }
  121. /**
  122. * 获取几天前零点到现在/昨日结束的时间戳
  123. *
  124. * @param int $day 天数
  125. * @param bool $now 返回现在或者昨天结束时间戳
  126. * @return array
  127. */
  128. public static function dayToNow($day = 1, $now = true)
  129. {
  130. $end = time();
  131. if (!$now) {
  132. list($foo, $end) = self::yesterday();
  133. }
  134. return [
  135. mktime(0, 0, 0, date('m'), date('d') - $day, date('Y')),
  136. $end
  137. ];
  138. }
  139. /**
  140. * 返回几天前的时间戳
  141. *
  142. * @param int $day
  143. * @return int
  144. */
  145. public static function daysAgo($day = 1)
  146. {
  147. $nowTime = time();
  148. return $nowTime - self::daysToSecond($day);
  149. }
  150. /**
  151. * 返回几天后的时间戳
  152. *
  153. * @param int $day
  154. * @return int
  155. */
  156. public static function daysAfter($day = 1)
  157. {
  158. $nowTime = time();
  159. return $nowTime + self::daysToSecond($day);
  160. }
  161. /**
  162. * 天数转换成秒数
  163. *
  164. * @param int $day
  165. * @return int
  166. */
  167. public static function daysToSecond($day = 1)
  168. {
  169. return $day * 86400;
  170. }
  171. /**
  172. * 周数转换成秒数
  173. *
  174. * @param int $week
  175. * @return int
  176. */
  177. public static function weekToSecond($week = 1)
  178. {
  179. return self::daysToSecond() * 7 * $week;
  180. }
  181. private static function startTimeToEndTime()
  182. {
  183. }
  184. }