php时间计算

这篇博客详细介绍了PHP在时间处理上的各种操作,包括获取日期的天、时、分、秒,计算月份总天数,求两个日期间隔,找出日期内周末天数,以及判断日期是否为周末的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PHP时间相关计算

1. 日期获取对应的天、时、分、秒:

    $startdate="2010-12-11 11:40:00";
    $enddate="2012-12-12 11:45:09";
    $date=floor((strtotime($enddate)-strtotime($startdate))/86400);
    $hour=floor((strtotime($enddate)-strtotime($startdate))%86400/3600);
    $minute=floor((strtotime($enddate)-strtotime($startdate))%86400/60);
    $second=floor((strtotime($enddate)-strtotime($startdate))%86400%60);
    echo $date."天<br>";
    echo $hour."小时<br>";
    echo $minute."分钟<br>";
    echo $second."秒<br>";  

2. 获取月份总天数:

 $days = date('t', strtotime($yearmonth)); 月份总天数

3. 获取两个日期间隔天数:

   $firstday = date('Y-m-01', strtotime($yearmonth));
   $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
   // 方法一:
   $diff = date_diff(date_create($start_day),date_create($end_day));
   echo $diff->format("%d");

   // 方法二:
    $total_days=floor((strtotime($lastday )-strtotime($firstday))/86400)+1; 
    echo $total_days;

4. 计算日期内的周末的天数

/**
     * 获取日期内的周末的天数
     * @param char|int $start_date 一个有效的日期格式,例如:20091016,2009-10-16
     * @param char|int $end_date 同上
     * @param BOOl $is_workday 返回休息日
     * @return 给定日期之间的周末天数
     */
    public static function GetWeekendDays($start_date,$end_date,$is_workday = false)
    {

        if (strtotime($start_date) > strtotime($end_date)) list($start_date, $end_date) = array($end_date,$start_date);
        $start_reduce = $end_add = 0;
        $start_N = date('N',strtotime($start_date));
        $start_reduce = ($start_N == 7) ? 1 : 0;
        $end_N = date('N',strtotime($end_date));
        in_array($end_N,array(6,7)) && $end_add = ($end_N == 7) ? 2 : 1;
        $alldays = abs(strtotime($end_date) - strtotime($start_date))/86400 + 1;
        $weekend_days = floor(($alldays + $start_N - 1 - $end_N) / 7) * 2 - $start_reduce + $end_add;
        if ($is_workday)
        {
            $workday_days = $alldays - $weekend_days;
            return $workday_days;
        }
        return $weekend_days;
    }

5. 获取两个日期之间的所有日期

 /**
     *  获取两个日期之间的所有日期
     * @param $strDateFrom 开始日期
     * @param $strDateTo 结束日期日期
     *
     */
    public static function GetDateRangeArray($strDateFrom, $strDateTo)
    {
        $firt_date = strtotime($strDateFrom) ? : '';
        $end_date = strtotime($strDateTo) ? : '';

        if (empty($firt_date))
            return '';
        if (empty($end_date))
            return '';

        $aryRange = array();

        if ($end_date >= $firt_date)
        {
            array_push($aryRange, date('Y-m-d', $firt_date));
            while ($firt_date < $end_date) {
                $firt_date += 86400; // add 24 hours
                array_push($aryRange, date('Y-m-d', $firt_date));
            }
        }
        return $aryRange;
    }

6. 判断是否是周末

 /**
     *  判断是否是周末
     * @param  $date string 日期,必须是时间格式
     */
    public static function isWeekend($date)
    {
        $day = strtotime($date);
        //判断是否是周六或周日
        $isWeekEnd = 0;
        $week = date("D",$day); //D - 一周中的某天(Mon - Sun)
        if ($week == 'Sat' || $week == 'Sun')
        {
            $isWeekEnd = 1;
        }
        return $isWeekEnd;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值