PHP date calculation

前端 未结 11 1682
不知归路
不知归路 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 09:16

    The following works for me. Believe I found it on the php.net docs somewhere.

    *Edit - Woops, didn't see csl's post. This is the exact function from his link, must have been where I found it. ;)

    //Find the difference between two dates
    function dateDiff($startDate, $endDate)
    {
        // Parse dates for conversion
        $startArry = date_parse($startDate);
        $endArry = date_parse($endDate);
    
        // Convert dates to Julian Days
        $start_date = gregoriantojd($startArry["month"], $startArry["day"], $startArry["year"]);
        $end_date = gregoriantojd($endArry["month"], $endArry["day"], $endArry["year"]);
    
        // Return difference
        return round(($end_date - $start_date), 0);
    }
    

提交回复
热议问题