时间处理

  1. 获取指定年、月、周、日起止秒位时间戳和时间

     # 区间全封闭,精确到秒
     function getStartEndTime(string $type, int $offset)
     {
         $offset = $offset < 0 ? ' ' . $offset : ' +' . $offset;
         switch ($type) {
             case 'year':
                 $result['start_time'] = strtotime(date('Y-01-01', time()) . "$offset year");
                 $result['end_time'] = strtotime(date('Y-01-01 23:59:59', time()) . "$offset year +1 year -1 day");
                 break;
             case 'month':
                 $result['start_time'] = strtotime(date('Y-m-01', time()) . "$offset month");
                 $result['end_time'] = strtotime(date('Y-m-01 23:59:59', time()) . "$offset month +1 month -1 day");
                 break;
             case 'week':
                 $result['start_time'] = strtotime(date('Y-m-d', strtotime('this week')) . "$offset week");
                 $result['end_time'] = strtotime(date('Y-m-d 23:59:59', strtotime('this week')) . "$offset week +1 week -1 day");
                 break;
             case 'day':
                 $result['start_time'] = strtotime(date('Y-m-d', time()) . "$offset day");
                 $result['end_time'] = strtotime(date('Y-m-d 23:59:59', time()) . "$offset day");
                 break;
             default :
                 $result['start_time'] = 0;
                 $result['end_time'] = time();
                 break;
         }
         $result['start_str'] = date('Y-m-d H:i:s', $result['start_time']);
         $result['end_str'] = date('Y-m-d H:i:s', $result['end_time']);
         return $result;
     }
    
     # 区间为左闭右开,精确到正无穷小
     function getStartEndTime(string $type, int $offset)
     {
         $offset = $offset < 0 ? ' ' . $offset : ' +' . $offset;
         switch ($type) {
             case 'year':
                 $result['start_time'] = strtotime(date('Y-01-01', time()) . "$offset year");
                 $result['end_time'] = strtotime(date('Y-01-01', time()) . "$offset year +1 year");
                 break;
             case 'month':
                 $result['start_time'] = strtotime(date('Y-m-01', time()) . "$offset month");
                 $result['end_time'] = strtotime(date('Y-m-01', time()) . "$offset month +1 month");
                 break;
             case 'week':
                 $result['start_time'] = strtotime(date('Y-m-d', strtotime('this week')) . "$offset week");
                 $result['end_time'] = strtotime(date('Y-m-d', strtotime('this week')) . "$offset week +1 week");
                 break;
             case 'day':
                 $result['start_time'] = strtotime(date('Y-m-d', time()) . "$offset day");
                 $result['end_time'] = strtotime(date('Y-m-d', time()) . "$offset day +1 day");
                 break;
             default :
                 $result['start_time'] = 0;
                 $result['end_time'] = time();
                 break;
         }
         $result['start_str'] = date('Y-m-d H:i:s', $result['start_time']);
         $result['end_str'] = date('Y-m-d H:i:s', $result['end_time']);
         return $result;
     }
    
  2. 获取指定范围起止日期列表

     # 起点到终点顺序为时间前推
     # $start:      相对于今天的起点,0-今天为起点,1-昨天为起点,以此类推
     # $days:       要取得的天数
     # $start_time: 开始的日期YYYY-MM-DD
     # $end_time:   结束的日期YYYY-MM-DD
     function getStartEndDate($start, $days, $start_time, $end_time)
     {
         $offset_day = (strtotime(date('Y-m-d'), time()) - strtotime($end_time)) / (24 * 3600);
         $start = 0 - $start - $offset_day;
         $result = [];
         for ($day = 0; $day < $days; $day++) {
             $offset = $start - $day;
             $offset = $offset < 0 ? ' ' . $offset : ' +' . $offset;
             $now_time = strtotime(date('Y-m-d', time()) . "$offset day");
             if ($now_time >= strtotime($start_time)) {
                 $result[] = date('Y-m-d', $now_time);
             }
         }
         return $result;
     }
    
@耿志环 2012-∞ 冀ICP备17033181号, powered by Gitbook修订: 2019-07-08 16:49:59

results matching ""

    No results matching ""