php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50916 DateTime::sub repeats each time getTimestamp is called
Submitted: 2010-02-02 18:04 UTC Modified: 2010-09-20 07:43 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: charlesb at itsmystuff dot com Assigned:
Status: Duplicate Package: Date/time related
PHP Version: 5.3.1 OS: Windows Server 2003 SP2
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: charlesb at itsmystuff dot com
New email:
PHP Version: OS:

 

 [2010-02-02 18:04 UTC] charlesb at itsmystuff dot com
Description:
------------
Each time DateTime::getTimestamp is called, a previously called DateTime::sub value is applied.  This does not occur with DateTime::add.

Reproduce code:
---------------
$work_time = new DateTime("2010-01-30 12:00:00"); 
$work_time->sub(new DateInterval("P7D"));
$window_start = $work_time->getTimestamp();
echo date("Y-m-d H:i:s", $window_start) . "<br />";
$window_start = $work_time->getTimestamp();
echo date("Y-m-d H:i:s", $window_start) . "<br />";

Expected result:
----------------
2010-01-23 12:00:00
2010-01-23 12:00:00

Actual result:
--------------
2010-01-16 12:00:00
2010-01-09 12:00:00

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-02-20 10:28 UTC] pontus dot alexander at gmail dot com
This bug is confirmed to still be alive in snapshot "5.3-201002200930" compiled on Debian Linux.

Reproduction code:
$Time = new \DateTime('NOW');
$Month = new \DateInterval('P1Y');

var_dump($Time);

// Correctly subtracted
$Time->sub($Month); 

var_dump($Time);

// Wrongly subtracted
$Time->getTimestamp();

var_dump($Time);
 [2010-03-02 23:00 UTC] v-ryanbi at microsoft dot com
I've attached a patch that is tested on Windows, although the code that needs to be changed appears to be universal. The patch also includes a .phpt that compares the resulting timestamps.

The fix for this is a bit disconcerting for me, but as I'm not entirely familiar with the codebase, I didn't want to muck things up too much.

Shouldn't the c function do_adjust_relative() in tm2unixtime.c be the one to clear the flags it uses after it applies changes? If we're clearing the flags in the calling functions, we're bound to miss more stuff like this one.
 [2010-03-10 09:00 UTC] yoarvi at gmail dot com
The fix for http://bugs.php.net/bug.php?id=49059 fixes this problem as well. I think this bug can be closed as a duplicate of 49059.
 [2010-03-10 09:15 UTC] aharvey@php.net
-Status: Open +Status: Duplicate
 [2010-09-20 07:33 UTC] david dot wright at opticsplanet dot com
Problem is still alive and kicking under 5.3.2.

I had it happen repetition of prior sub() right after a call to getTimeStamp().
Easily the most aggravating "not-my-bug-but-the-language-itself" I've ever 
encountered.
 [2010-09-20 07:43 UTC] rasmus@php.net
Sure, if you follow the links here you will see it was fixed in 5.3.3 which has 
been out for 2 months now.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC