DateHelper.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace common\helpers;
  3. use Yii;
  4. /**
  5. * 日期助手类
  6. */
  7. class DateHelper
  8. {
  9. /**
  10. * 日期列表
  11. * @param string $beginDatetime 开始日期,日期时间格式,如2017-08-28 15:11:39
  12. * @param string $endDatetime 结束日期
  13. * @return array
  14. */
  15. public static function getDaily($beginDatetime, $endDatetime)
  16. {
  17. $beginTime = strtotime($beginDatetime);
  18. if (!$beginTime) {
  19. return [];
  20. }
  21. $endTime = strtotime($endDatetime);
  22. if (!$endTime) {
  23. return [];
  24. }
  25. $list = [];
  26. while ($beginTime <= $endTime) {
  27. $list[] = date('Y-m-d', $beginTime);
  28. $beginTime = strtotime('+1 days midnight', $beginTime);
  29. }
  30. return $list;
  31. }
  32. /**
  33. * 从昨天起,日期列表
  34. * @param string $date 日期,日期时间格式,如2017-08-28 15:11:39
  35. * @return array
  36. */
  37. public static function getDailyYesterday($date)
  38. {
  39. $now = strtotime(date("Y-m-d",strtotime('-1 days')));
  40. $start = strtotime($date);
  41. $temp = $start;
  42. $list = [];
  43. $i = 0;
  44. while ($now >= $temp) {
  45. $temp = strtotime("+$i days", $start);
  46. $list[] = date('Y-m-d', $temp);
  47. $i++;
  48. }
  49. return $list;
  50. }
  51. /**
  52. * 把时间戳转成日期时间格式
  53. * @param int $timestamp
  54. * @return string
  55. */
  56. public static function convertTime($timestamp)
  57. {
  58. $timestamp = (int) $timestamp;
  59. if (!$timestamp) {
  60. return '';
  61. }
  62. $datetime = date('Y-m-d H:i:s', $timestamp);
  63. if (!$datetime) {
  64. return '';
  65. }
  66. return $datetime;
  67. }
  68. /**
  69. * 把格式化的本地日期转成GMT/UTC(即0时区)的日期
  70. *
  71. * $local_date = '2018-03-07 15:44:00';
  72. *
  73. * date_default_timezone_set('Asia/Shanghai');
  74. * var_dump(date_default_timezone_get());
  75. * var_dump(convertDate($local_date));
  76. *
  77. * date_default_timezone_set('UTC');
  78. * var_dump(date_default_timezone_get());
  79. * var_dump(convertDate($local_date));
  80. *
  81. * @param string $local_date
  82. * @param int $timezone 参数$local_date对应的时区
  83. * @return string
  84. */
  85. public static function convertDateToGmt($local_date, $timezone = 8)
  86. {
  87. $time = strtotime(trim($local_date));
  88. if (!$time) {
  89. return $local_date;
  90. }
  91. $time = $time - $timezone * 3600;
  92. $date = date('Y-m-d H:i:s', $time);
  93. return $date;
  94. }
  95. /**
  96. * 等同于convertDateToGmt
  97. * @param string $local_date
  98. * @param int $timezone 参数$local_date对应的时区
  99. * @return string
  100. */
  101. public static function convertDateToUtc($local_date, $timezone = 8)
  102. {
  103. return static::convertDateToGmt($local_date, $timezone);
  104. }
  105. /**
  106. * 把GMT/UTC(即0时区)的日期转成格式化的本地日期
  107. * @param string $gmt_date
  108. * @param int $timezone
  109. * @return string
  110. */
  111. public static function convertDateToLocal($gmt_date, $timezone = 8)
  112. {
  113. $time = strtotime(trim($gmt_date));
  114. if (!$time) {
  115. return $gmt_date;
  116. }
  117. $time = $time + $timezone * 3600;
  118. $date = date('Y-m-d H:i:s', $time);
  119. return $date;
  120. }
  121. }