PHP 今日、昨日、本周、上周、本月常用起始时间戳和结束时间戳的时间处理类

Jackey PHP 3,921 次浏览 , , 没有评论
  1. <?php
  2. class Time
  3. {
  4. /**
  5.   * 返回今日开始和结束的时间戳
  6.   *
  7.   * @return array
  8.   */
  9. public static function today()
  10. {
  11. return [
  12. mktime(0, 0, 0, date('m'), date('d'), date('Y')),
  13. mktime(23, 59, 59, date('m'), date('d'), date('Y'))
  14. ];
  15. }
  16.  
  17. /**
  18.   * 返回昨日开始和结束的时间戳
  19.   *
  20.   * @return array
  21.   */
  22. public static function yesterday()
  23. {
  24. $yesterday = date('d') - 1;
  25. return [
  26. mktime(0, 0, 0, date('m'), $yesterday, date('Y')),
  27. mktime(23, 59, 59, date('m'), $yesterday, date('Y'))
  28. ];
  29. }
  30.  
  31. /**
  32.   * 返回本周开始和结束的时间戳
  33.   *
  34.   * @return array
  35.   */
  36. public static function week()
  37. {
  38. $timestamp = time();
  39. return [
  40. strtotime(date('Y-m-d', strtotime("this week Monday", $timestamp))),
  41. strtotime(date('Y-m-d', strtotime("this week Sunday", $timestamp))) + 24 * 3600 - 1
  42. ];
  43. }
  44.  
  45. /**
  46.   * 返回上周开始和结束的时间戳
  47.   *
  48.   * @return array
  49.   */
  50. public static function lastWeek()
  51. {
  52. $timestamp = time();
  53. return [
  54. strtotime(date('Y-m-d', strtotime("last week Monday", $timestamp))),
  55. strtotime(date('Y-m-d', strtotime("last week Sunday", $timestamp))) + 24 * 3600 - 1
  56. ];
  57. }
  58.  
  59. /**
  60.   * 返回本月开始和结束的时间戳
  61.   *
  62.   * @return array
  63.   */
  64. public static function month($everyDay = false)
  65. {
  66. return [
  67. mktime(0, 0, 0, date('m'), 1, date('Y')),
  68. mktime(23, 59, 59, date('m'), date('t'), date('Y'))
  69. ];
  70. }
  71.  
  72. /**
  73.   * 返回上个月开始和结束的时间戳
  74.   *
  75.   * @return array
  76.   */
  77. public static function lastMonth()
  78. {
  79. $begin = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
  80. $end = mktime(23, 59, 59, date('m') - 1, date('t', $begin), date('Y'));
  81.  
  82. return [$begin, $end];
  83. }
  84.  
  85. /**
  86.   * 返回今年开始和结束的时间戳
  87.   *
  88.   * @return array
  89.   */
  90. public static function year()
  91. {
  92. return [
  93. mktime(0, 0, 0, 1, 1, date('Y')),
  94. mktime(23, 59, 59, 12, 31, date('Y'))
  95. ];
  96. }
  97.  
  98. /**
  99.   * 返回去年开始和结束的时间戳
  100.   *
  101.   * @return array
  102.   */
  103. public static function lastYear()
  104. {
  105. $year = date('Y') - 1;
  106. return [
  107. mktime(0, 0, 0, 1, 1, $year),
  108. mktime(23, 59, 59, 12, 31, $year)
  109. ];
  110. }
  111.  
  112. /**
  113.   * 获取几天前零点到现在/昨日结束的时间戳
  114.   *
  115.   * @param int $day 天数
  116.   * @param bool $now 返回现在或者昨天结束时间戳
  117.   * @return array
  118.   */
  119. public static function dayToNow($day = 1, $now = true)
  120. {
  121. $end = time();
  122. if (!$now) {
  123. list($foo, $end) = self::yesterday();
  124. }
  125.  
  126. return [
  127. mktime(0, 0, 0, date('m'), date('d') - $day, date('Y')),
  128. $end
  129. ];
  130. }
  131.  
  132. /**
  133.   * 返回几天前的时间戳
  134.   *
  135.   * @param int $day
  136.   * @return int
  137.   */
  138. public static function daysAgo($day = 1)
  139. {
  140. $nowTime = time();
  141. return $nowTime - self::daysToSecond($day);
  142. }
  143.  
  144. /**
  145.   * 返回几天后的时间戳
  146.   *
  147.   * @param int $day
  148.   * @return int
  149.   */
  150. public static function daysAfter($day = 1)
  151. {
  152. $nowTime = time();
  153. return $nowTime + self::daysToSecond($day);
  154. }
  155.  
  156. /**
  157.   * 天数转换成秒数
  158.   *
  159.   * @param int $day
  160.   * @return int
  161.   */
  162. public static function daysToSecond($day = 1)
  163. {
  164. return $day * 86400;
  165. }
  166.  
  167. /**
  168.   * 周数转换成秒数
  169.   *
  170.   * @param int $week
  171.   * @return int
  172.   */
  173. public static function weekToSecond($week = 1)
  174. {
  175. return self::daysToSecond() * 7 * $week;
  176. }
  177. }
  178.  
  179. ?>

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Go