123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- namespace time;
- use think\Exception;
- use think\Log;
- class DateHelper
- {
-
- public static function isDate($dateString)
- {
-
- return strtotime(date('Y-m', strtotime($dateString))) === strtotime($dateString);
- }
-
- public static function isDateYmd($date)
- {
- if (preg_match("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts)) {
- return checkdate($parts[2], $parts[3], $parts[1]);
- } else {
- return false;
- }
- }
-
- public static function dateFormat($dateTime, $format = 'Y-m-d H:i:s')
- {
- $nowtime = strtotime($dateTime);
- return date($format, $nowtime);
- }
-
- public static function timeTip($startdate)
- {
- if ($startdate) {
- if (is_numeric($startdate)) {
- $startdate = date('Y-m-d H:i:s', $startdate);
- }
- $enddate = date('Y-m-d H:i:s');
- $date = floor((strtotime($enddate) - strtotime($startdate)) / 86400);
- $hour = floor((strtotime($enddate) - strtotime($startdate)) % 86400 / 3600);
- $minute = floor((strtotime($enddate) - strtotime($startdate)) % 86400 % 3600 / 60);
- $second = floor((strtotime($enddate) - strtotime($startdate)) % 86400 % 60);
- if ($date > 90) {
- return '很久前';
- } elseif ($date >= 30 && $date <= 90) {
- return floor($date / 30) . '个月前';
- } elseif ($date > 0 && $date < 30) {
- return $date . '天前';
- } elseif ($hour < 24 && $hour > 0) {
- return $hour . '小时前';
- } elseif ($minute < 60 && $minute > 0) {
- return $minute . '分钟前';
- } elseif ($second < 60 && $second > 0) {
- return $second . '秒前';
- } else {
- return '刚刚';
- }
- }
- return "null";
- }
-
- public static function dateTimeList($startTime, $endTime, $inclued = false, $format = 'Y-m-d', $interval = 'tomorrow')
- {
- $starttime = strtotime($startTime);
- $endtime = strtotime($endTime);
-
- $i = 0;
- $arr = [];
- if ($inclued) {
- $arr[] = date($format, $starttime);
- }
- while (($starttime = strtotime($interval, $starttime)) < $endtime) {
- $arr[] = date($format, $starttime);
- if ($i++ > 500) {
- Log::error($arr);
- throw new Exception('循环超过限制');
- }
- }
- if ($inclued) {
- $arr[] = date($format, $endtime);
- }
- return $arr;
- }
-
- public static function splitTimePeriod($start, $end, $count, $format = 'H:i:s')
- {
- if ($count <= 0) {
- return [];
- }
- $second_total = strtotime($end) - strtotime($start);
- $arr = [];
- if ($count > 1) {
- $interval = floor($second_total / $count);
- $time_period = DateHelper::dateTimeList($start, $end, false, $format, "+ $interval seconds");
- foreach ($time_period as $k => $period) {
- if ($k == 0) {
- $arr[] = $start . " - " . $period;
- } else {
- $arr[] = $time_period[$k - 1] . " - " . $period;
- }
- }
- $arr[] = end($time_period) . " - " . $end;
- } else {
- $arr[] = $start . " - " . $end;
- }
- return $arr;
- }
- }
|