php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79403 DateTime::construct inconsistent results with "first day of"
Submitted: 2020-03-22 02:52 UTC Modified: 2022-07-22 16:41 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: nfoskett at internode dot on dot net Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 7.4.4 OS: Linux
Private report: No CVE-ID: None
 [2020-03-22 02:52 UTC] nfoskett at internode dot on dot net
Description:
------------
When using "first day of" relative time formats, the constructor gives inconsistent values for what ought to be the same. Results shown below are all as of 2020-03-22 03:49:52.

Test script:
---------------
$month_start = new DateTime("first day of this month");
echo $month_start->format('Y-m-d H:i:s') . "\n";
$month_start = new DateTime("first day of March");
echo $month_start->format('Y-m-d H:i:s') . "\n";

https://3v4l.org/lG3GL

Expected result:
----------------
2020-03-01 00:00:00
2020-03-01 00:00:00

or possibly

2020-03-01 03:49:52
2020-03-01 03:49:52


Actual result:
--------------
2020-03-01 03:49:52
2020-03-01 00:00:00


Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-04-07 12:23 UTC] alexinbeijing at gmail dot com
Interesting little bug. Looking at the date parsing code, it appears that this code only works by accident:

new DateTime("first day of this month")

This actually returns the exact same result:

new DateTime("first day of this year")
 [2020-04-08 08:15 UTC] alexinbeijing at gmail dot com
The following pull request has been associated:

Patch Name: Fix #79403: DateTime::construct inconsistent results with "first day of"
On GitHub:  https://github.com/php/php-src/pull/5360
Patch:      https://github.com/php/php-src/pull/5360.patch
 [2022-07-22 16:41 UTC] derick@php.net
-Status: Open +Status: Not a bug
 [2022-07-22 16:41 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

I'm going to mark this as "expected behaviour". The parser simply can't understand everything. If you still find the need to want to add this support, file a new ticket at our GitHub repository.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Dec 11 09:01:28 2024 UTC