简体   繁体   中英

Date Difference in Days doesn't calculate correctly

I am not sure if I am going crazy or have missed something simple. I am simply trying to get the difference between 2 dates in days but it doesn't show what I think it should.

I am currently using the following code

$firstDate  = new DateTime("06/09/2021");
$secondDate = new DateTime("05/01/2022");
$intvl = $firstDate->diff($secondDate);

// Total amount of days
echo $intvl->days . " days ";

This returns: 326 days but it should be 121...

Am I doing something wrong here, any help is appreciated!

Your input dates are ambiguous: is "06/09/2021" the 6th September (as a UK reader would assume) or 9th June (as a US reader would assume)? The US interpretation leads to the 326 days PHP is returning; the UK interpretation leads to the 121 days you were expecting.

It's better to either specify the format you're expecting, with DateTime::createFromFormat(...) , or to use an unambiguous format, such as "2021-09-06".

You might also want to get into the habit of using the DateTimeImmutable class, which is generally less confusing to work with.

So:

$firstDate  = DateTimeImmutable::createFromFormat("d/m/Y", "06/09/2021");
$secondDate = DateTimeImmutable::createFromFormat("d/m/Y", "05/01/2022");
$intvl = $firstDate->diff($secondDate);
echo $intvl->days . " days ";
// 121 days

$firstDate  = DateTimeImmutable::createFromFormat("m/d/Y", "06/09/2021");
$secondDate = DateTimeImmutable::createFromFormat("m/d/Y", "05/01/2022");
$intvl = $firstDate->diff($secondDate);
echo $intvl->days . " days ";
// 326 days

$firstDate  = new DateTimeImmutable("2021-09-06");
$secondDate  = new DateTimeImmutable("2022-01-05");
$intvl = $firstDate->diff($secondDate);
echo $intvl->days . " days ";
// 121 days

9th June 2021 to 1st May 2022 is definitely 326 days.

By any chance are you expecting that PHP will interpret these as 6th September and 5th January? If we read the documentation for the DateTime constructor, the linked article shows us the formats which it can parse by default. Based on what you've provided, it will assume it's m/d/Y rather than d/m/Y .

If you need to parse other formats which aren't supported by the default parser, then you need to use the createFromFormat function to generate the DateTime object, rather than the constructor.

eg

$firstDate  = DateTime::createFromFormat("d/m/Y", "06/09/2021");
$secondDate = DateTime::createFromFormat("d/m/Y", "05/01/2022");
$intvl = $firstDate->diff($secondDate);

// Total amount of days
echo $intvl->days . " days ";

Demo: http://sandbox.onlinephpfunctions.com/code/2f1689615f5813dbf699d92c70ad1679a9a96e4f


Alternatively, to avoid any ambiguity, it's generally a good idea wherever possible to generate and pass your date strings around in an ISO8601-compatible format which has no other recognised (mis-)interpretations, eg Ymd :

$firstDate  = new DateTime("2021-09-06");
$secondDate = new DateTime("2022-01-05");
$intvl = $firstDate->diff($secondDate);

// Total amount of days
echo $intvl->days . " days ";

Demo: http://sandbox.onlinephpfunctions.com/code/506cbe95dee91736bcc1e33ae57d02cdaf258114

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM