php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71690 DateTime::createFromFormat malfunctions on a leap day
Submitted: 2016-02-29 14:01 UTC Modified: 2016-02-29 17:31 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: stefan at forward dot cat Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 7.0.3 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: stefan at forward dot cat
New email:
PHP Version: OS:

 

 [2016-02-29 14:01 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-02-29 17:05 UTC] 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 17:13 UTC] 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:31 UTC] derick@php.net
-Status: Open +Status: Not a bug
 [2016-02-29 17:31 UTC] derick@php.net
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
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Feb 05 12:01:32 2025 UTC