PHP date calculation

前端 未结 11 1689
不知归路
不知归路 2021-01-01 08:19

What is the best (date format independent way) in PHP to calculate difference in days between two dates in specified format.

I tried the following function:

11条回答
  •  轮回少年
    2021-01-01 08:54

    PHP 5.2 introduces the DateTime and DateInterval classes which makes this easy:

    function get_date_offset($start_date, $end_date)
    {
        $start_time = new DateTime($start_date);
        $end_time   = new DateTime($end_date);
        $interval   = $end_time->diff($start_time);
        return $interval->format('%a days');
    }
    

提交回复
热议问题