datetime - PHP DateInterval calculation number of months incorrect
Get the solution ↓↓↓Since the PHP timezone was changed in a project,\DateTime::diff
does not longer calculate the number of months between two dates correctly.
Here is some example code to reproduce the problem:
<pre>
<?php
$tz = new \DateTimeZone('UTC');
$start = new \DateTime('2020-04-01', $tz);
$end = new \DateTime('2020-04-30', $tz);
$end->modify('+1 days');
$diff = $end->diff($start);
print "Result with UTC TimeZone:\n";
print_r($diff);
// $diff[m] == 1 and $diff[d] == 0
//---With Different Timezone (Europe/Berlin)
$tz = new \DateTimeZone('Europe/Berlin');
$start = new \DateTime('2020-04-01', $tz);
$end = new \DateTime('2020-04-30', $tz);
$end->modify('+1 days');
$diff = $end->diff($start);
print "\n\nResult with Europe/Berlin TimeZone:\n";
print_r($diff);
//---------> Returns: $diff[m] == 0 and $diff[d] == 30
//With Different Timezone and Time 00:00:00 (Europe/Berlin)
$tz = new \DateTimeZone('Europe/Berlin');
$start = new \DateTime('2020-04-01 00:00:00', $tz);
$end = new \DateTime('2020-04-30 00:00:00', $tz);
$end->modify('+1 days');
$diff = $end->diff($start);
print "\n\nResult with Europe/Berlin TimeZone and 00:00 Time:\n";
print_r($diff);
//---------> Returns: $diff[m] == 0 and $diff[d] == 30
//---With Different Timezone and Time Part and Time 10:00:00 (Europe/Berlin)
$tz = new \DateTimeZone('Europe/Berlin');
$start = new \DateTime('2020-04-01 10:00:00', $tz);
$end = new \DateTime('2020-04-30 10:00:00', $tz);
$end->modify('+1 days');
$diff = $end->diff($start);
print "\n\nResult with Europe/Berlin TimeZone and 10:00 Time:\n";
print_r($diff);
//---------> Returns: $diff[m] == 1 and $diff[d] == 0
Any idea? Is this a bug in the PHP Date Lib?
I'm using 8.0.6.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the metadata storage is not up to date, please run the sync-metadata-storage command to fix this issue.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.