php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62784 DateTime: "last day of" with timestamps is buggy
Submitted: 2012-08-09 07:58 UTC Modified: 2022-05-13 13:41 UTC
Votes:5
Avg. Score:3.4 ± 0.8
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:1 (25.0%)
From: daniel dot rudolf at eifel-online dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 5.3.15 OS: Windows XP
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: daniel dot rudolf at eifel-online dot com
New email:
PHP Version: OS:

 

 [2012-08-09 07:58 UTC] daniel dot rudolf at eifel-online dot com
Description:
------------
Using new DateTime("last day of @timestamp") doesn't work properly

Test script:
---------------
<?php
$time = new DateTime('2012-01-01 00:00:00 UTC');
$time = $time->getTimestamp();
$monthEnd = new DateTime('last day of @'.$time);
echo date('Y-m-d', $time)." --> ".$monthEnd->format('Y-m-d')."\n";

/* ----------------------------------- */

$time = new DateTime('now'); // today = 2012-08-09
$time = $time->getTimestamp();
$monthEnd = new DateTime('last day of @'.$time);
echo date('Y-m-d', $time)." --> ".$monthEnd->format('Y-m-d')."\n";

/* ----------------------------------- */

$time = new DateTime('2020-12-12');
$time = $time->getTimestamp();
$monthEnd = new DateTime('last day of @'.$time);
echo date('Y-m-d', $time)." --> ".$monthEnd->format('Y-m-d')."\n";

/* ----------------------------------- */

$time = new DateTime('2000-02-01'); // leapyear
$time = $time->getTimestamp();
$monthEnd = new DateTime('last day of @'.$time);
echo date('Y-m-d', $time)." --> ".$monthEnd->format('Y-m-d')."\n";

/* ----------------------------------- */

$time = new DateTime('2000-02-01 00:00:01 UTC');
$time = $time->getTimestamp();
$monthEnd = new DateTime('last day of @'.$time);
echo date('Y-m-d', $time)." --> ".$monthEnd->format('Y-m-d')."\n";
?>

Expected result:
----------------
2012-01-01 --> 2012-01-31
2012-08-09 --> 2012-08-31
2020-12-12 --> 2020-12-31
2000-02-01 --> 2000-02-29
2000-02-01 --> 2000-02-29


Actual result:
--------------
2012-01-01 --> 2012-01-31
2012-08-09 --> 2012-09-08
2020-12-12 --> 2021-01-10
2000-02-01 --> 2000-03-01
2000-02-01 --> 2000-03-02


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-09 08:04 UTC] daniel dot rudolf at eifel-online dot com
Note that only timestamps are affected!

<?php
$time = new DateTime('2000-02-01 00:00:01 UTC');
$time = $time->getTimestamp();
$monthEnd = new DateTime('last day of '.date('r', $time));
echo date('Y-m-d', $time)." --> ".$monthEnd->format('Y-m-d')."\n";
// Expected and actual result: 2000-02-01 --> 2000-02-29 
?>
 [2022-05-13 13:41 UTC] derick@php.net
The phrase "last day of @7289347234" really does not have any meaning, so I am not sure whether it makes sense to do anything here. I am closing this, as this would increase the parser complexity significantly, with a reasonable workaround available. If you have a really compelling case, feel free to add a ticket to https://github.com/php/php-src/issues, but it needs to have a really good reason for this to get added.
 [2022-05-13 13:41 UTC] derick@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: derick
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Feb 05 08:01:30 2025 UTC