| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- namespace common\helpers;
- use Yii;
- /**
- * 日期助手类
- */
- class DateHelper
- {
- /**
- * 日期列表
- * @param string $beginDatetime 开始日期,日期时间格式,如2017-08-28 15:11:39
- * @param string $endDatetime 结束日期
- * @return array
- */
- public static function getDaily($beginDatetime, $endDatetime)
- {
- $beginTime = strtotime($beginDatetime);
- if (!$beginTime) {
- return [];
- }
- $endTime = strtotime($endDatetime);
- if (!$endTime) {
- return [];
- }
- $list = [];
- while ($beginTime <= $endTime) {
- $list[] = date('Y-m-d', $beginTime);
- $beginTime = strtotime('+1 days midnight', $beginTime);
- }
- return $list;
- }
- /**
- * 从昨天起,日期列表
- * @param string $date 日期,日期时间格式,如2017-08-28 15:11:39
- * @return array
- */
- public static function getDailyYesterday($date)
- {
- $now = strtotime(date("Y-m-d",strtotime('-1 days')));
- $start = strtotime($date);
- $temp = $start;
- $list = [];
- $i = 0;
- while ($now >= $temp) {
- $temp = strtotime("+$i days", $start);
- $list[] = date('Y-m-d', $temp);
- $i++;
- }
- return $list;
- }
- /**
- * 把时间戳转成日期时间格式
- * @param int $timestamp
- * @return string
- */
- public static function convertTime($timestamp)
- {
- $timestamp = (int) $timestamp;
- if (!$timestamp) {
- return '';
- }
- $datetime = date('Y-m-d H:i:s', $timestamp);
- if (!$datetime) {
- return '';
- }
- return $datetime;
- }
-
- /**
- * 把格式化的本地日期转成GMT/UTC(即0时区)的日期
- *
- * $local_date = '2018-03-07 15:44:00';
- *
- * date_default_timezone_set('Asia/Shanghai');
- * var_dump(date_default_timezone_get());
- * var_dump(convertDate($local_date));
- *
- * date_default_timezone_set('UTC');
- * var_dump(date_default_timezone_get());
- * var_dump(convertDate($local_date));
- *
- * @param string $local_date
- * @param int $timezone 参数$local_date对应的时区
- * @return string
- */
- public static function convertDateToGmt($local_date, $timezone = 8)
- {
- $time = strtotime(trim($local_date));
- if (!$time) {
- return $local_date;
- }
-
- $time = $time - $timezone * 3600;
-
- $date = date('Y-m-d H:i:s', $time);
- return $date;
- }
-
- /**
- * 等同于convertDateToGmt
- * @param string $local_date
- * @param int $timezone 参数$local_date对应的时区
- * @return string
- */
- public static function convertDateToUtc($local_date, $timezone = 8)
- {
- return static::convertDateToGmt($local_date, $timezone);
- }
-
- /**
- * 把GMT/UTC(即0时区)的日期转成格式化的本地日期
- * @param string $gmt_date
- * @param int $timezone
- * @return string
- */
- public static function convertDateToLocal($gmt_date, $timezone = 8)
- {
- $time = strtotime(trim($gmt_date));
- if (!$time) {
- return $gmt_date;
- }
-
- $time = $time + $timezone * 3600;
-
- $date = date('Y-m-d H:i:s', $time);
- return $date;
- }
-
- }
|