php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #69661 date_parse says the day is 1 instead of false
Submitted: 2015-05-19 11:17 UTC Modified: 2022-05-13 14:49 UTC
Votes:3
Avg. Score:2.7 ± 1.2
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: php-bugs at xpaw dot me Assigned:
Status: Wont fix Package: Date/time related
PHP Version: 5.6.9 OS: Ubuntu
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: php-bugs at xpaw dot me
New email:
PHP Version: OS:

 

 [2015-05-19 11:17 UTC] php-bugs at xpaw dot me
Description:
------------
I don't know if this is expected behaviour or not, but parsing a date without a day, will produce a result where 'day' will be set to 1. If this is expected, there really should be a parameter that disables this. I'm using this for user provided data, and it's not really valid.

Test script:
---------------
var_dump(date_parse("May 2013"));

Expected result:
----------------
  ["day"]=>
  bool(false)


Actual result:
--------------
  ["day"]=>
  int(1)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-19 11:54 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2015-05-19 11:54 UTC] cmb@php.net
See also <http://3v4l.org/eAiXb>.
 [2017-01-04 17:04 UTC] contact at frank-liepert dot de
This is expected behaviour, see "Textual month and four digit year (Day reset to 1)" on http://php.net/manual/de/datetime.formats.date.php
 [2018-02-14 18:27 UTC] cmb@php.net
-Status: Verified +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2018-02-14 18:27 UTC] cmb@php.net
Indeed, this is documented behavior!
 [2018-02-14 18:48 UTC] php-bugs at xpaw dot me
Documented behaviour does not make it correct behaviour.

Currently I have no way of testing if the day field is default or was actually parsed from the input.
 [2018-02-14 22:08 UTC] cmb@php.net
-Status: Not a bug +Status: Re-Opened -Type: Bug +Type: Feature/Change Request -Assigned To: cmb +Assigned To:
 [2018-02-14 22:08 UTC] cmb@php.net
Okay, changing to feature request.
 [2022-05-13 14:49 UTC] derick@php.net
-Status: Re-Opened +Status: Wont fix
 [2022-05-13 14:49 UTC] derick@php.net
If you want more control over parsing dates, where the behaviour of strtotime/ the DateTime constructor does not apply, you can use the date_parse_from_format function, like:

php -r 'var_dump(date_parse_from_format("M Y", "May 2013"));'

This will then show you the right information:

array(12) {
  'year' =>
  int(2013)
  'month' =>
  int(5)
  'day' =>
  bool(false)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 11:01:30 2024 UTC