一些日期的计算方式 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;
    }
扫描二维码关注公众号,回复: 8895445 查看本文章
发布了37 篇原创文章 · 获赞 2 · 访问量 3112

猜你喜欢

转载自blog.csdn.net/weixin_41406041/article/details/100571589