|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 17 07:00:01 2025 UTC |
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 ?>