0

I've got the following DateTime object in php:

[start1] => DateTime Object ( 
    [date] => 2012-05-21 12:59:59
    [timezone_type] => 3
    [timezone] => Europe/Berlin
)

[end1] => DateTime Object ( 
    [date] => 2012-05-21 22:36:00
    [timezone_type] => 3
    [timezone] => Europe/Berlin
)

and the result of:

$time->end1->diff($time->start1

is:

DateInterval Object ( [y] => 0 [m] => 0 [d] => 0 [h] => 12 [i] => 36 [s] => 2 [invert] => 1 [days] => 0 )

Why do I get an period of 12 hours and not 9?

Cœur
  • 37,241
  • 25
  • 195
  • 267
soupdiver
  • 3,504
  • 9
  • 40
  • 68
  • What about the seconds? Shouldn't it be 1 instead of 2? Maybe it has to do something with the timezone type? Try changing that value and see what happens. – Andrius Naruševičius May 21 '12 at 14:44
  • Your code got chopped off. Please post the complete line beginning `$time->end1->diff($time->start1` – Jonathan M May 21 '12 at 14:47
  • Try `date_default_timezone_set('Europe/Berlin');` before running the diff. – bfavaretto May 21 '12 at 14:52
  • date_default_timezone_set('Europe/Berlin'); didn't worked. And there is nothing chopped of. Only the bracket is missing and the seconds are also ok. – soupdiver May 21 '12 at 15:54

2 Answers2

2

I've found the solution...Before doing the diff I do a sub() on the start1 object. Now I've seen why my result is false... Here is the answer but I REALLY don't why this is happening. http://www.php.net/manual/en/datetime.sub.php#101175

soupdiver
  • 3,504
  • 9
  • 40
  • 68
0

I understand that this may in fact be what you do to create the two DateTime objects BUT I figured I would put what worked for me to see if perhaps it would help you out.

To create the two objects I did:

$start1 = new DateTime('2012-05-21 12:59:59', new DateTimeZone('Europe/Berlin'));
$end1   = new DateTime('2012-05-21 22:36:00', new DateTimeZone('Europe/Berlin'));

And printing out the two objects and the diff:

print_r($end1->diff($start1));

I got:

DateTime Object
(
    [date] => 2012-05-21 12:59:59
    [timezone_type] => 3
    [timezone] => Europe/Berlin
)
DateTime Object
(
    [date] => 2012-05-21 22:36:00
    [timezone_type] => 3
    [timezone] => Europe/Berlin
)
DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 0
    [h] => 9
    [i] => 36
    [s] => 1
    [invert] => 1
    [days] => 0
)

I see that both of our outputs for the start1 and end1 objects is the same, but my diff reflects the correct 9 hour difference. Perhaps there's something wonky about how you create the DateTime object in your version of PHP?

EmmanuelG
  • 1,051
  • 9
  • 14