0

I found a link earlier regarding using time diffs and getting the difference in minutes, hours and days: How to get time difference in minutes in PHP

I was trying this:

    $date1 = new DateTime('first day of this month', new DateTimeZone('Europe/Amsterdam'));
    $date2 = new DateTime('first day of this month', new DateTimeZone('Europe/London'));

    print_r($date1->format('Y-m-d H:i:s'));
    print_r($date2->format('Y-m-d H:i:s'));

The output was like:

    2013-12-01 13:00:36
    2013-12-01 12:00:36

Then used this:

    $diff = $date2->diff($date1);
    print_r($diff);

But then i get 0 in all the differences. I want to get the difference between the two without using strtotime.. I is it outputing 0?

Community
  • 1
  • 1

3 Answers3

3

Your expectation doesn't make sense, since there is no difference. 2013-12-01 13:00:36 Amsterdam and 2013-12-01 12:00:36 London are the exact same point in time in human history. What you appear to expect is the offset difference between the London and Amsterdam timezones (i.e. GMT and GMT+1 differ by 1), but that has nothing to do with concrete timestamps.

deceze
  • 510,633
  • 85
  • 743
  • 889
1

You want to calculate the offset.Use DateTimeZone::getOffset()

$dateTimeZoneAmsterdam = new DateTimeZone("Europe/Amsterdam");
$dateTimeZoneLondon = new DateTimeZone("Europe/London");


$dateTimeAmsterdam = new DateTime('first day of this month', $dateTimeZoneAmsterdam);
$dateTimeLondon = new DateTime('first day of this month', $dateTimeZoneLondon);


$timeOffset = $dateTimeZoneAmsterdam->getOffset($dateTimeLondon);

print_r($timeOffset); // 3600
Chris
  • 4,255
  • 7
  • 42
  • 83
0

You are close. Try DateTime::diff

Elliptical view
  • 3,338
  • 1
  • 31
  • 28