Bug #71530 [Com]: wrong dateTime diff results for 01.03.2016 and 01.03.2017

From: Date: Fri, 18 Mar 2016 10:04:27 +0000
Subject: Bug #71530 [Com]: wrong dateTime diff results for 01.03.2016 and 01.03.2017
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-199916@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71530&edit=1 ID: 71530 Comment by: ryosuke_i_628 at yahoo dot co dot jp Reported by: pawel dot zegardlo at bm dot pl Summary: wrong dateTime diff results for 01.03.2016 and 01.03.2017 Status: Open Type: Bug Package: Date/time related Operating System: windows 8.1 64bit PHP Version: 5.6.18 Block user comment: N Private report: N New Comment: A related bug is reported here: http://qiita.com/smzk/items/0b8c47dbdb283832a94b It works on UTC, but doesn't on Asia/Tokyo. Test script: --------------- <?php $patterns = [ ['2016-01-01', '2016-02-01'], ['2016-02-01', '2016-03-01'], ['2016-03-01', '2016-04-01'], ['2016-02-01', '2016-02-28'], ['2016-03-01', '2016-03-31'], ['2016-04-01', '2016-04-30'], ]; $timezones = ['UTC', 'Asia/Tokyo']; foreach ($timezones as $timezone) { foreach ($patterns as $pattern) { $d1 = new DateTime($pattern[0], new DateTimeZone($timezone)); $d2 = new DateTime($pattern[1], new DateTimeZone($timezone)); $diff = $d1->diff($d2); printf( "There are %d months %2d days between %s and %s (%s)\n", $diff->m, $diff->d, $pattern[0], $pattern[1], $timezone ); } echo "\n"; } Expected result: ---------------- There are 1 months 0 days between 2016-01-01 and 2016-02-01 (UTC) There are 1 months 0 days between 2016-02-01 and 2016-03-01 (UTC) There are 1 months 0 days between 2016-03-01 and 2016-04-01 (UTC) There are 0 months 27 days between 2016-02-01 and 2016-02-28 (UTC) There are 0 months 30 days between 2016-03-01 and 2016-03-31 (UTC) There are 0 months 29 days between 2016-04-01 and 2016-04-30 (UTC) There are 1 months 0 days between 2016-01-01 and 2016-02-01 (Asia/Tokyo) There are 1 months 0 days between 2016-02-01 and 2016-03-01 (Asia/Tokyo) There are 1 months 0 days between 2016-03-01 and 2016-04-01 (Asia/Tokyo) There are 0 months 27 days between 2016-02-01 and 2016-02-28 (Asia/Tokyo) There are 0 months 30 days between 2016-03-01 and 2016-03-31 (Asia/Tokyo) There are 0 months 29 days between 2016-04-01 and 2016-04-30 (Asia/Tokyo) Actual result: -------------- There are 1 months 0 days between 2016-01-01 and 2016-02-01 (UTC) There are 1 months 0 days between 2016-02-01 and 2016-03-01 (UTC) There are 1 months 0 days between 2016-03-01 and 2016-04-01 (UTC) There are 0 months 27 days between 2016-02-01 and 2016-02-28 (UTC) There are 0 months 30 days between 2016-03-01 and 2016-03-31 (UTC) There are 0 months 29 days between 2016-04-01 and 2016-04-30 (UTC) There are 1 months 0 days between 2016-01-01 and 2016-02-01 (Asia/Tokyo) There are 0 months 29 days between 2016-02-01 and 2016-03-01 (Asia/Tokyo) There are 1 months 2 days between 2016-03-01 and 2016-04-01 (Asia/Tokyo) There are 0 months 27 days between 2016-02-01 and 2016-02-28 (Asia/Tokyo) There are 1 months 1 days between 2016-03-01 and 2016-03-31 (Asia/Tokyo) There are 0 months 29 days between 2016-04-01 and 2016-04-30 (Asia/Tokyo) Previous Comments: ------------------------------------------------------------------------ [2016-02-05 09:11:56] pawel dot zegardlo at bm dot pl Description: ------------ I've tested this on v5.6.15, but in changelog from 5.6.15 to 5.6.18 there is nothing about datetime so this bug seems to be not fixed also in 5.6.18. If we change the month in a sample code from march to april, the result is the same for both comparisons (1 year 0 days 0 months). Also if we change the day in sample code from 01 to 02 and stay with march, the result is also the same. But for original code attached, the result is different but should be also the same. Test script: --------------- <?php $start = new DateTime('01.03.2016'); $end = new DateTime('01.03.2017'); $diff = $end->diff($start); var_dump($start->format('d-m-Y'), $end->format('d-m-Y'), $diff); $start = new DateTime('02.03.2016'); $end = new DateTime('02.03.2017'); $diff = $end->diff($start); var_dump($start->format('d-m-Y'), $end->format('d-m-Y'), $diff); Expected result: ---------------- string '01-03-2016' (length=10) string '01-03-2017' (length=10) object(DateInterval)[3] public 'y' => int 1 public 'm' => int 0 public 'd' => int 0 public 'h' => int 0 public 'i' => int 0 public 's' => int 0 public 'weekday' => int 0 public 'weekday_behavior' => int 0 public 'first_last_day_of' => int 0 public 'invert' => int 1 public 'days' => int 365 public 'special_type' => int 0 public 'special_amount' => int 0 public 'have_weekday_relative' => int 0 public 'have_special_relative' => int 0 string '02-03-2016' (length=10) string '02-03-2017' (length=10) object(DateInterval)[2] public 'y' => int 1 public 'm' => int 0 public 'd' => int 0 public 'h' => int 0 public 'i' => int 0 public 's' => int 0 public 'weekday' => int 0 public 'weekday_behavior' => int 0 public 'first_last_day_of' => int 0 public 'invert' => int 1 public 'days' => int 365 public 'special_type' => int 0 public 'special_amount' => int 0 public 'have_weekday_relative' => int 0 public 'have_special_relative' => int 0 Actual result: -------------- string '01-03-2016' (length=10) string '01-03-2017' (length=10) object(DateInterval)[3] public 'y' => int 0 public 'm' => int 11 public 'd' => int 28 public 'h' => int 0 public 'i' => int 0 public 's' => int 0 public 'weekday' => int 0 public 'weekday_behavior' => int 0 public 'first_last_day_of' => int 0 public 'invert' => int 1 public 'days' => int 365 public 'special_type' => int 0 public 'special_amount' => int 0 public 'have_weekday_relative' => int 0 public 'have_special_relative' => int 0 string '02-03-2016' (length=10) string '02-03-2017' (length=10) object(DateInterval)[2] public 'y' => int 1 public 'm' => int 0 public 'd' => int 0 public 'h' => int 0 public 'i' => int 0 public 's' => int 0 public 'weekday' => int 0 public 'weekday_behavior' => int 0 public 'first_last_day_of' => int 0 public 'invert' => int 1 public 'days' => int 365 public 'special_type' => int 0 public 'special_amount' => int 0 public 'have_weekday_relative' => int 0 public 'have_special_relative' => int 0 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71530&edit=1

« previous php.bugs (#199916) next »