graczu_-
graczu_-

Reputation: 11

PHP datetime difference in months return bad values, mis php configuration or a bug

So im using in my script datetime and diff method for months difference calculating, and i create test script to show you what is my problem.

Runing on my server (remote linux and local windows in home, timezones Europe/Warsaw):

echo "StartDate\tEndDate\tMonthNum\tDifference\n";
for($i = 1; $i <= 12; $i++){
        $filters['date_from'] = "2020-$i-01";
        $filters['date_to'] = date("Y-m-t", strtotime("2020-$i-01"));
        $datetime1 = new DateTime($filters['date_from']);
        $datetime2 = new DateTime($filters['date_to']);
        $difference = $datetime1->diff($datetime2);
        $end = $difference->m;
            echo "{$filters['date_from']}\t{$filters['date_to']}\t" . $i . "\t$end\n";
}

and result's with PHP 7.1.29 and PHP 7.4.1:

StartDate       EndDate MonthNum        Difference
2020-1-01       2020-01-31      1       0
2020-2-01       2020-02-29      2       0
2020-3-01       2020-03-31      3       1
2020-4-01       2020-04-30      4       0
2020-5-01       2020-05-31      5       1
2020-6-01       2020-06-30      6       0
2020-7-01       2020-07-31      7       1
2020-8-01       2020-08-31      8       0
2020-9-01       2020-09-30      9       0
2020-10-01      2020-10-31      10      1
2020-11-01      2020-11-30      11      0
2020-12-01      2020-12-31      12      1

so as you see diff showing 0 or 1 moth difference, 1 if month have more than 30 days, and 0 if less.

So i tested this code on http://sandbox.onlinephpfunctions.com/ and result's are diffrent:

StartDate   EndDate MonthNum    Difference
2020-1-01   2020-01-31  1   0
2020-2-01   2020-02-29  2   0
2020-3-01   2020-03-31  3   0
2020-4-01   2020-04-30  4   0
2020-5-01   2020-05-31  5   0
2020-6-01   2020-06-30  6   0
2020-7-01   2020-07-31  7   0
2020-8-01   2020-08-31  8   0
2020-9-01   2020-09-30  9   0
2020-10-01  2020-10-31  10  0
2020-11-01  2020-11-30  11  0
2020-12-01  2020-12-31  12  0

All month's have 0 months difference so it's working good, maybe somone know what can be the problem over my server?

Upvotes: 1

Views: 138

Answers (1)

jspit
jspit

Reputation: 7683

Yes, that's a bug. The difference from the first day of the month to the last day of the month (00:00) should always return 0 month, because the last day is missing. 1 month is from the first day of the month (00:00) to the first day of the following month at 00:00. The error is caused by an incorrect time zone shift to UTC. The months are calculated correctly if the time zone UTC is used as a workaround.

$tz = new dateTimeZone('UTC');
for($i = 1; $i <= 12; $i++){
  $n = $i+1;
  $dateFrom = "2020-$i-01";
  $dateTo = $n == 13 ? "2021-01-01" : "2020-$n-01";
  $diff = date_create($dateFrom,$tz)->diff(date_create($dateTo,$tz));
  echo $dateFrom." to ".$dateTo." = ".$diff->m. " month or ".$diff->days." days<br>\n";
}

Output:

2020-1-01 to 2020-2-01 = 1 month or 31 days
2020-2-01 to 2020-3-01 = 1 month or 29 days
2020-3-01 to 2020-4-01 = 1 month or 31 days
2020-4-01 to 2020-5-01 = 1 month or 30 days
2020-5-01 to 2020-6-01 = 1 month or 31 days
2020-6-01 to 2020-7-01 = 1 month or 30 days
2020-7-01 to 2020-8-01 = 1 month or 31 days
2020-8-01 to 2020-9-01 = 1 month or 31 days
2020-9-01 to 2020-10-01 = 1 month or 30 days
2020-10-01 to 2020-11-01 = 1 month or 31 days
2020-11-01 to 2020-12-01 = 1 month or 30 days
2020-12-01 to 2021-01-01 = 1 month or 31 days

Upvotes: 2

Related Questions