• 一些日期的计算方式 PHP


    一些日期的计算

     某个月内的所有天数:

        public function getMonthDay ($date)
        {
            $stattime = strtotime(date('Ym01',strtotime($date .'01')));
            $day      = date('t',strtotime($date .'01'));
            $i = 0;
            $arr = [];
            while ($i < $day) {
                $arr[$i]['datetime'] = date('Ymd',$stattime + $i * 86400);
                $i++;
            }
    
            return $arr;
        }

    今日:图表X轴以小时为单位,每隔3小时显示一个数值。

     public function getToday ($date)
        {
            //今日:图表X轴以小时为单位,每隔3小时显示一个数值。
            $today    = strtotime ($date);
            $todayEnd = strtotime ($date . '+1 day');
            $hours = ($todayEnd - $today) / (3 * 3600);
    
            $i = 0;
            $arr = [];
            while ($i < $hours) {
                $arr[$i]['starthour'] = date ('YmdH' , $today + $i * 3 * 3600);
                $arr[$i]['endhour'] = date ('YmdH' , $today + (($i + 1) * 3 -1) * 3600);
                $i++;
            }
            return $arr;
        }

    昨日:图表X轴以小时为单位,每隔3小时显示一个数值。

      public function getYesterday ($date)
        {
            //昨日:图表X轴以小时为单位,每隔3小时显示一个数值。
            $today     = strtotime ($date);
            $yesterday = strtotime ($date .'-1 day');
            $hours = ($today - $yesterday) / (3 * 3600);
    
            $i = 0;
            $arr = [];
            while ($i < $hours) {
                $arr[$i]['starthour'] = date ('YmdH' , $yesterday + $i * 3* 3600);
                $arr[$i]['endhour'] = date ('YmdH' , $yesterday + (($i + 1) * 3 -1)* 3600);
                $i++;
            }
    
            return $arr;
        }

    近1月:图表X轴以天为单位,每隔7天显示一个数值。

    public function getMonth ($date)
    {
        //近1月:图表X轴以天为单位,每隔7天显示一个数值。
        $mon = date ('Ym01' , strtotime ($date));
    
        $month = strtotime(date('Ym01',strtotime($mon .'-1 month'))); //上个月第一天
        $monthend = strtotime(date ('Y-m-t',$month)); // 上个月最后一天
    
        $days = floor(($monthend - $month) / (7 * 24 * 3600));
        if ($days<4) {
            $days = 4; // 28天的月份
        }
        $i = 0;
        $arr = [];
        while ($i < $days) {
            $arr[$i]['startdays'] = date ('Ymd' , $month + $i * 7 * 24 * 3600);
            if($days==$i+1){
                $arr[$i]['enddays'] = date('Ymd',$monthend);//上个月最后一天
            }else{
                $arr[$i]['enddays'] = date ('Ymd' , $month + (($i + 1) * 7-1) * 24 * 3600);
            }
            $i++;
        }
    
        return $arr;
    }

     近6月:图表X轴以月为单位,每个月份显示一个数值。

     public function getSixMonth ($date)
        {
            //近6月:图表X轴以月为单位,每个月份显示一个数值。
            $i = 0;
            $arr = [];
            $date = date('Y-m-01',strtotime($date));
            while ($i < 6) {
                $arr[$i]['startmonth'] = date ('Ym' , strtotime ($date .'-'. (6 - $i) . ' month'));
                $arr[$i]['endmonth']   = date ('Ym' , strtotime ($date .'-'.(6 - $i) . ' month'));
                $i++;
            }
            return $arr;
        }
  • 相关阅读:
    linux mysql添加用户名并实现远程访问
    bootstrap-datetimepicker时间控件的使用
    jquery图片左右来回循环飘动
    jquery 全选获取值
    设置linux编码utf-8
    nginx 自签名https
    Laravel 邮件配置
    memcachq队列安装
    开发与运维使用常用工具
    composer配置和安装php框架
  • 原文地址:https://www.cnblogs.com/xdtx/p/9473184.html
Copyright © 2020-2023  润新知