|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-07-27 11:46 UTC] ben dot davies at stickyeyes dot com
Description:
------------
If you create a DateTime object using a relative date string of "this week +6 days", when calling getTimestamp on the datetime object, the date is mysteriously increments by one day. However, if you seperately modify the datetime using 'this week', then '+6 days', it works correctly.
Where is the additional day comeing from, and why is it only applied when calling format('U') DOESNT increment the datetime by 1 day. Wierd!
Here is a work around:
$endOfWeek = new DateTime();
$endOfWeek->modify('this week');
$endOfWeek->modify('+6 days');
echo $endOfWeek->format('Y-m-d H:m:s')."\n";
echo $endOfWeek->format('U')."\n";
/* Thar she blows! */
echo $endOfWeek->getTimestamp()."\n";
echo $endOfWeek->format('Y-m-d H:m:s')."\n";
Test script:
---------------
$endOfWeek = new DateTime();
$endOfWeek->modify('this week +6 days');
echo $endOfWeek->format('Y-m-d H:m:s')."\n";
echo $endOfWeek->format('U')."\n";
/* Thar she blows! */
echo $endOfWeek->getTimestamp()."\n";
echo $endOfWeek->format('Y-m-d H:m:s')."\n";
Expected result:
----------------
DateTime should stay the same after a call to getTimestamp (surely getTimestamp should be safe from side effects)
Patches52454_date_get_timestamp.patch (last revision 2010-08-20 10:03 UTC by mike at iammike dot co dot uk)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 06:00:02 2025 UTC |
I believe I've encountered the same bug although with slightly different effects. Code to reproduce ----------------- date_default_timezone_set("Europe/London"); $s = new DateTime; $e = new DateTime("+3 months"); $i = new DateInterval("P1M"); $p = new DatePeriod($s, $i, $e); foreach($p as $m) { $m->getTimestamp(); $m->getTimestamp(); echo $m->format("Y-m-d") . "\n"; } Expected results ---------------- 2010-08-20 2010-09-20 2010-10-20 Actual results -------------- 2010-08-20 2010-11-20 2010-12-20 I first encountered this in PHP 5.3.2-1ubuntu4.2 but I've verified it's still there in PHP 5.3.99-dev (trunk snapshot 201008200830). Additional calls to getTimestamp cause the month to be incremented further. Interestingly, if you swap "+3 months" for "+3 years" and "P1M" for "P1Y" the year is affected instead of the month.