php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55461 date_modify() affects variables recursive?!
Submitted: 2011-08-19 12:07 UTC Modified: 2011-08-19 12:16 UTC
From: nico at is55 dot de Assigned:
Status: Not a bug Package: Date/time related
PHP Version: Irrelevant OS: Win7
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: nico at is55 dot de
New email:
PHP Version: OS:

 

 [2011-08-19 12:07 UTC] nico at is55 dot de
Description:
------------
---
From manual page: http://www.php.net/function.date-modify
---
I'm sorry if I did do something wrong and waste your time, but I can't explain this behaviour:


date_create() gets assigned to a variable >>> $now

this variable ($now) is being copied to another variable ($later) and this new variable ($later) then modified with date_modify() to be 30 days in the past

ODD BEHAVIOUR:
after this is executed BOTH variables, $now and $later are 30 days in the past?

Test script:
---------------
<?php
$now = date_create(); # get current datetime

$later = $now; # copy $now to $later
date_modify($later,'-30 day');

$now_str = date_format($now,'Y-m-d H:i:s');	
$later_str = date_format($later,'Y-m-d H:i:s');

echo "\$now_str: ".$now_str."</br>";
echo "\$later_str: ".$later_str."</br>";
?>

/*
$now_str: 2011-07-20 22:05:31
$later_str: 2011-07-20 22:05:31
*/


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-08-19 12:16 UTC] laruence@php.net
-Status: Open +Status: Bogus
 [2011-08-19 12:16 UTC] laruence@php.net
since $now is a DateTime object, 

you should use :
  $later = clone $now; 

to accomplish your requirement.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri May 09 21:01:27 2025 UTC