Bug #71687 [Opn->Nab]: Adding one year to a leap day produces incorrect date

From: Date: Mon, 29 Feb 2016 12:05:44 +0000
Subject: Bug #71687 [Opn->Nab]: Adding one year to a leap day produces incorrect date
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-199524@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71687&edit=1 ID: 71687 Updated by: requinix@php.net Reported by: kungfutse at hotmail dot com Summary: Adding one year to a leap day produces incorrect date -Status: Open +Status: Not a bug Type: Bug Package: Date/time related Operating System: Linux PHP Version: 5.6.18 Block user comment: N Private report: N New Comment: PHP's* date arithmetic for P1Y literally increments the year by one. That would create the date 2017-02-29 which overflows into 2017-03-01. This method doesn't appear to be suitable for your needs; you may have to do the date math yourself, such as by comparing the number of days in the beginning and end months, seeing that 29 is beyond next year's 28, and creating the date {$year+1}-{$month}-{$number_of_days} = 2017-02-28 manually. * Python, Perl, and IIRC Linux's date do this overflow stuff too. Previous Comments: ------------------------------------------------------------------------ [2016-02-29 10:41:34] kungfutse at hotmail dot com Description: ------------ When adding one year (new DateInterval('P1Y')) to a leap day (new DateTime('2016-02-29')) results in incorrect date. It should return 2017-02-28 but returns 2017-03-01 instead. For comparison, MariaDB 10 returns the correct date when doing SELECT DATE_ADD(DATE('2016-02-29'), INTERVAL 1 YEAR); Test script: --------------- var_dump( (new \DateTime('2016-02-29'))->add(new \DateInterval('P1Y')) ); Expected result: ---------------- object(DateTime)#1 (3) { ["date"]=> string(26) "2017-02-28 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(15) "Europe/Helsinki" } Actual result: -------------- object(DateTime)#1 (3) { ["date"]=> string(26) "2017-03-01 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(15) "Europe/Helsinki" } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71687&edit=1

« previous php.bugs (#199524) next »