| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- <?php
- namespace common\helpers;
- /**
- * 日期数据格式返回
- *
- * Class DateHelper
- * @package common\helpers
- * @author qimall
- */
- class DateHelper
- {
- /**
- * 获取今日开始时间戳和结束时间戳
- *
- * 语法:mktime(hour,minute,second,month,day,year) => (小时,分钟,秒,月份,天,年)
- */
- public static function today()
- {
- return [
- 'start' => mktime(0, 0, 0, date('m'), date('d'), date('Y')),
- 'end' => mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1,
- ];
- }
- /**
- * 昨日
- *
- * @return array
- */
- public static function yesterDay()
- {
- return [
- 'start' => mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')),
- 'end' => mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1,
- ];
- }
- /**
- * @name:
- * @msg: 获取前几天 or 后几天的开始 和 结束时间戳
- * @param {int} $number 偏离天数
- * @param {string} $type 获取类型, before:从今天往前拿;after:从今天往后拿
- * @return array
- */
- public static function someDays(int $number, string $type = 'before')
- {
- if ('after' == $type) {
- $end = mktime(0, 0, 0, date('m'), date('d') + $number, date('Y')) - 1;
- $start = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
- } else {
- $start = mktime(0, 0, 0, date('m'), date('d') - $number, date('Y'));
- $end = strtotime(date('Ymd', $start) . ' 23:59:59');
- }
- return compact('start', 'end');
- }
- /**
- * 这周
- *
- * @return array
- */
- public static function thisWeek()
- {
- return [
- 'start' => mktime(0, 0, 0, date('m'), date('d') - date('w') + 1, date('Y')),
- 'end' => mktime(23, 59, 59, date('m'), date('d') - date('w') + 7, date('Y')),
- ];
- }
- /**
- * 上周
- *
- * @return array
- */
- public static function lastWeek()
- {
- return [
- 'start' => mktime(0, 0, 0,date('m'),date('d') - date('w') + 1 - 7, date('Y')),
- 'end' => mktime(23, 59, 59,date('m'),date('d') - date('w') + 7 - 7, date('Y')),
- ];
- }
- /**
- * 本月
- *
- * @return array
- */
- public static function thisMonth()
- {
- return [
- 'start' => mktime(0, 0, 0, date('m'), 1, date('Y')),
- 'end' => mktime(23, 59, 59, date('m'), date('t'), date('Y')),
- ];
- }
- /**
- * 上个月
- *
- * @return array
- */
- public static function lastMonth()
- {
- $start = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
- $end = mktime(23, 59, 59, date('m') - 1, date('t',$start), date('Y'));
- if (date('m', $start) != date('m', $end))
- {
- $end -= 60 * 60 * 24;
- }
- return [
- 'start' => $start,
- 'end' => $end,
- ];
- }
- public static function lastYear()
- {
- $year = date("Y",strtotime("-1 year"));
- $start = strtotime($year.'-01-01 00:00:00');
- $end = strtotime($year.'-12-31 23:59:59');
- return [
- 'start' => $start,
- 'end' => $end,
- ];
- }
- public static function lastSeason(){
- $season = ceil((date('n'))/3)-1;//上季度是第几季度
- return [
- 'start'=>strtotime(date('Y-m-d H:i:s', mktime(0, 0,0,$season*3-3+1,1,date('Y')))),
- 'end'=>strtotime(date('Y-m-d H:i:s',mktime(23,59,59,$season*3,date('t',mktime(0, 0 ,0,$season*3,1,date("Y"))),date('Y')))),
- ];
- }
- /**
- * 几个月前
- *
- * @param integer $month 月份
- * @return array
- */
- public static function monthsAgo($month)
- {
- return [
- 'start' => mktime(0, 0, 0, date('m') - $month, 1, date('Y')),
- 'end' => mktime(23, 59, 59, date('m') - $month, date('t'), date('Y')),
- ];
- }
- public static function endOfMonth($month){
- return mktime(23,59,59,date('m',strtotime($month))+1,00); //指定月份月末时间戳
- }
- public static function thisSeason(){
- $season = ceil((date('n'))/3);//当月是第几季度
- return [
- 'start' => mktime(0, 0, 0,$season*3-3+1,1,date('Y')),
- 'end' =>mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y')),
- ];
- }
- /**
- * 格式化时间戳
- *
- * @param $time
- * @return string
- */
- public static function formatTimestamp($time)
- {
- $min = $time / 60;
- $hours = $time / 60;
- $days = floor($hours / 24);
- $hours = floor($hours - ($days * 24));
- $min = floor($min - ($days * 60 * 24) - ($hours * 60));
- return $days . " 天 " . $hours . " 小时 " . $min . " 分钟 ";
- }
- /**
- * 时间戳
- *
- * @param integer $accuracy 精度 默认微妙
- * @return int
- */
- public static function getMicrotime($accuracy = 1000000)
- {
- $microtime = explode(' ', microtime());
- return $microtime = (int)round(($microtime[1] + $microtime[0]) * $accuracy, 0);
- }
- /**
- * 时间格式
- * @param int $time
- * @return string 2023-11-09(周四) 18:30
- */
- public static function reserveAt(int $time)
- {
- $week_txt = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
- $week = date('w', $time);
- return date('Y-m-d', $time) . "($week_txt[$week]) " . date("H:i", $time);
- }
- /**
- * 获取日期中文名
- * @param int $time
- * @return string
- */
- public static function dayChineseText(int $time)
- {
- $texts = ['今天', ' 明天', '后天'];
- $week_txt = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
- $index = -1;
- for ($i = 0; $i < 3; $i++) {
- $i_time = strtotime(date('Y-m-d', time() + ($i * 86400)));
- if ($i_time == $time) {
- $index = $i;
- break;
- }
- }
- if (!empty($texts[$index])) return $texts[$index];
- return date("Y-m-d", $time) . "({$week_txt[date('w', $time)]}) " . date("H:i", $time);
- }
- public static function getMonthLastDayTime($start_time)
- {
- $mdays = date('t', $start_time);
- return strtotime(date('Y-m-' . $mdays . ' 23:59:59', $start_time));
- }
- public static function getWeekByDate($date)
- {
- $week_txt = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
- $week = date('w', strtotime($date));
- return $week_txt[$week];
- }
- /**
- * 10000 转为 10:00
- * @param $intHourMin
- * @return false|string
- */
- public static function intHourMinToTime($intHourMin)
- {
- $time = str_pad($intHourMin, 4, '0', STR_PAD_LEFT);
- return date('H:i', strtotime("20241225{$time}00"));
- }
- public static function time2Int($time)
- {
- return (int) str_replace(':', '', $time);
- }
- public static function formatTime2Custom($timestamp)
- {
- $hour = date('H', $timestamp); // 获取小时部分
- // 根据时间段判断对应的中文表示
- if ($hour >= 0 && $hour < 6) {
- $timePeriod = '凌晨';
- } elseif ($hour >= 6 && $hour < 9) {
- $timePeriod = '早上';
- } elseif ($hour >= 9 && $hour < 12) {
- $timePeriod = '上午';
- } elseif ($hour == 12) {
- $timePeriod = '中午';
- } elseif ($hour > 12 && $hour < 18) {
- $timePeriod = '下午';
- } else {
- $timePeriod = '晚上';
- }
- $w = self::getWeekByDate(date('Y-m-d', $timestamp));
- // 格式化时间
- return date('Y-m-d', $timestamp) . ' ' . $timePeriod . ' ' . date('H:i', $timestamp);
- }
- public static function formatTime2CustomW($timestamp)
- {
- $hour = date('H', $timestamp); // 获取小时部分
- // 根据时间段判断对应的中文表示
- if ($hour >= 0 && $hour < 6) {
- $timePeriod = '凌晨';
- } elseif ($hour >= 6 && $hour < 9) {
- $timePeriod = '早上';
- } elseif ($hour >= 9 && $hour < 12) {
- $timePeriod = '上午';
- } elseif ($hour == 12) {
- $timePeriod = '中午';
- } elseif ($hour > 12 && $hour < 18) {
- $timePeriod = '下午';
- } else {
- $timePeriod = '晚上';
- }
- $w = self::getWeekByDate(date('Y-m-d', $timestamp));
- // 格式化时间
- return date('Y-m-d', $timestamp) . ' ('. $w . ') ' . $timePeriod;
- }
- }
|