php - Wrong DateInterval with DateTime diff -


i'm comparing 2 datetime :

//get current datetime $date_now = new datetime; var_dump($date_now); // 2014-03-10 19:04:29  // need subtract 12 hours (i'm using $date_past fetch $db_date in request) $date_past = $date_now->sub(new dateinterval("pt12h"));  // datetime $db_date fetched in database $alert_date = new datetime($db_date); var_dump($alert_date); // 2014-03-10 17:04:00 in test  // difference $diff = $alert_date->diff($date_now); var_dump($diff->format("%h:%i:%s")); //09:59:31 

so 09:59:31 , $diff->invert == 1 means it's negative value.

i saw issue : php datetime->diff calcualting wrong amount of hours (3 much) , think it's similar problem can't find solution keep using ->sub(). ideas ?

if think timezone problem, checked datetime , have timezone set "europe/berlin", don't think come here.

thank !

the problem when assign object variable, assigned by reference. when do:

$date_past = $date_now->sub(new dateinterval("pt12h")); 

the variables $date_past , $date_now both point same reference , when modify one, modify both. notice if echo out variables:

$date_now = new datetime; $date_past = $date_now->sub(new dateinterval("pt12h"));  echo $date_now->format('c'); // 2014-03-10t00:38:56-06:00 echo $date_past->format('c'); // 2014-03-10t00:38:56-06:00 

notice how both print same thing. want use clone:

$date_past = clone $date_now; $date_past->sub(new dateinterval("pt12h"));  echo $date_now->format('c'); // 2014-03-10t12:41:20-06:00 echo $date_past->format('c'); // 2014-03-10t00:41:20-06:00 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -