Bug #80918 [Opn->Nab]: Create from format does'nt work for february

From: Date: Tue, 30 Mar 2021 12:00:18 +0000
Subject: Bug #80918 [Opn->Nab]: Create from format does'nt work for february
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-233063@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80918&edit=1

 ID:                 80918
 Updated by:         derick@php.net
 Reported by:        steven dot woelk at papendorf dot info
 Summary:            Create from format does'nt work for february
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Date/time related
 Operating System:   Windows
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system
is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

This is expected, albeit perhaps surprising.

By default, all the individual fields are filled in with the current date and time:

2021-03-30 11:51

The createFromFormat('Y-m') overrides the year and month, resulting in:

2021-02-30 11:51

Of course there is no february 30th, and PHP will overflow:

2021-03-02 11:51

If you don't want to pre-fill in the values with the current date/time, use the ! operator as
you can read about at https://www.php.net/manual/en/datetime.createfromformat.php
:

\DateTime::createFromFormat('!Y-m');


Previous Comments:
------------------------------------------------------------------------
[2021-03-30 11:55:03] steven dot woelk at papendorf dot info

Description:
------------
---
From manual page: https://php.net/datetime.createfromformat
---

Consider a script with the following line:

$currentDate = \DateTime::createFromFormat('Y-m', $something);

Most time it will work but for 2021-02 for instance the returned value would be 2021-03-02
11:51:47.000000.

This is not a timestamp in february 2021.

Test script:
---------------
$currentDate = \DateTime::createFromFormat('Y-m', '2021-02');

Expected result:
----------------
$currentDate = \DateTime::createFromFormat('Y-m', '2021-02');

$currentDate->format('Y-m') === '2021-02'

Actual result:
--------------
$currentDate = \DateTime::createFromFormat('Y-m', '2021-02');

$currentDate->format('Y-m') === '2021-03'


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=80918&edit=1


Thread (2 messages)

« previous php.bugs (#233063) next »