简体   繁体   中英

DateTime::diff returns unexpected result

I have the following code that is returning an unexpected answer. Please let me know what's wrong.

$start_date  = new DateTime('31-03-2019');
$end_date    = new DateTime('01-05-2019');
$d = $start_date->diff($end_date);
echo "day: " . $d->d . " month: " . $d->m . "\n";

It is returning the following output:

day: 0 month: 1

I expect the output to be:

day: 1 month: 1

This will give you one day and one month https://3v4l.org/q0T8r

$start_date  = new DateTime('31-03-2019 00:00:00');
$end_date    = new DateTime('01-05-2019 24:00:00');
$d = $start_date->diff($end_date);
echo "day: " . $d->d . " month: " . $d->m . "\n";

When you add 1 month to 2019-03-31 , PHP will internally just increment the month value 03 to 04 . The result is 2019-04-31 .

As April has only 30 days, 2019-04-31 has the same meaning as 2019-05-01 has. And that's the reason, why you get one month and zero days as the result.


The DateInterval class has another handy property: days instead of m and d . It will contain the total number of days between the two dates, which equals to 31 (you have to add 31 days to 2019-03-31 to get to the 2019-05-01 .

On this value you can implement your own logic, what "one month" is. If you define it as "one month = 30 days", this could be your whished result:

$start_date  = new DateTime('31-03-2019');
$end_date    = new DateTime('01-05-2019');
$diff = $start_date->diff($end_date);

$months = floor($diff->days / 30);
$days = $diff->days % 30;

echo "day: " . $days . " month: " . $months . "\n";

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