Bug #71690 [Opn->Nab]: DateTime::createFromFormat malfunctions on a leap day

From: Date: Mon, 29 Feb 2016 17:31:37 +0000
Subject: Bug #71690 [Opn->Nab]: DateTime::createFromFormat malfunctions on a leap day
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-199530@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71690&edit=1 ID: 71690 Updated by: derick@php.net Reported by: stefan at forward dot cat Summary: DateTime::createFromFormat malfunctions on a leap day -Status: Open +Status: Not a bug Type: Bug Package: Date/time related PHP Version: 7.0.3 Block user comment: N Private report: N New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php This is the right behaviour. Any not-parsed date item (year, month, day, hour, min, second) is initialised with the current values. If you want to reset them to 1970-1-1 0:0:0, then you need to add the ! specifier: Compare: php -r 'var_dump(date_create_from_format("Y-m", "2015-02"));' to php -r 'var_dump(date_create_from_format("!Y-m", "2015-02"));' This is documented at http://php.net/manual/en/datetime.createfromformat.php Previous Comments: ------------------------------------------------------------------------ [2016-02-29 17:13:32] supraguy at yandex dot com I am having a similar issue as well. In my code if you do the following today (2016-02-29) var_dump(date('Y-m', strtotime('now -11 months'))); var_dump(date('Y-m', strtotime('now -12 months'))); The both return the same result, string(7) "2015-03" PHP 5.4.45 ------------------------------------------------------------------------ [2016-02-29 17:05:43] stefan at forward dot cat This should say: Expected result: ---------------- A date in february Actual result: -------------- 2015-03-01 12:21:30 ------------------------------------------------------------------------ [2016-02-29 14:01:13] stefan at forward dot cat Description: ------------ See test script, DateTime::createFromFormat("Y-m", "2015-02") creates a date in march (March 1 2015) when run today 29/2/2016. possibly related: https://bugs.php.net/bug.php?id=70956&edit=2 Test script: --------------- <?php $timezone = new DateTimeZone("UTC"); $result = DateTime::createFromFormat("Y-m", "2015-02", $timezone); $timestamp = $result->format('U'); $date_time = date_create('@' . $timestamp); date_timezone_set($date_time, $timezone); echo date_format($date_time, 'Y-m-d H:i:s'); Expected result: ---------------- 2015-03-01 12:21:30 Actual result: -------------- not a date in march ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71690&edit=1

« previous php.bugs (#199530) next »