addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobegmailgooglegroupshelp-with-circleimageimagesinstagramlinklocation-pinm-swarmSearchmailmessagesminusmoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruserwarningyahoo

Re: paul sent you a message: php basics question... adding datetime

From: John C.
Sent on: Saturday, September 29, 2012 3:04 PM
I am copying the group on this because I think it will be beneficial for many members of the group.

Paul, the reason that this happened is because of how PHP handles objects.  $today is a pointer to the object.  When you say $almost_tomorrow = $today, you are really saying they are the same object, so if you change one, you change them both.

What you really wanted to do was create a clone of the object.  Which is an entire copy.

I think changing line #2 to this will solve your problem.

$almost_tomorrow = clone $today;

Thanks for the question, and I hope this is clear.

John Congdon

On Sep 28, 2012, at 11:26 PM, Meetup <[address removed]> wrote:

Meetup Message from: paul, Member of San Diego PHP

php basics question... adding datetime

ok, so I thought you can simply use '=' to set a datetime equal to another datetime, but then it seems like a permanent assignment to a variable. is this a bug in PHP or is this correct? 

for example, 
$ today = new DateTime(); 
$ almost_tommorrow = $ today; 
echo "<br/>Today is = ".date_format($ today,'Y-m-d H:i:s')."<br/> 
Almost Tomorrow is = ".date_format($ almost_tommorrow,'Y-m-d H:i:s')."<br/>"; 
date_add($ almost_tommorrow, date_interval_create_from_date_string('24 hours')); 
echo "<br/>Today is = ".date_format($ today,'Y-m-d H:i:s')."<br/>"; 
Almost Tomorrow is = ".date_format($ almost_tommorrow,'Y-m-d H:i:s')."<br/>"; 

where here it ALSO modifies "$ today" value, even though it wasn't executed in any function?
This message was sent to you and 3 other organizers of San Diego PHP.
To report abuse or block this person, please click here.
Add [address removed] to your address book to receive all Meetup emails
To manage your email settings, click here
Meetup HQ in NYC is hiring!

Our Sponsors

  • Zeeto

    Huge thanks to Zeeto for hosting the SDPHP Meetup.

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy