Jump to content

Getting Time Difference


mostafatalebi

Recommended Posts

  • 2 weeks later...

I'm not sure how you mean. From your question and the link I gave I am guessing that you now have something like this

 

   function myTimeDiff ($d1, $d2)
   {
    $dt1 = new DateTime($d1);
    return $dt1->diff(new DateTime($d2))->format('%a Days %h Hours');
   }

$date_1 = '2013-02-01 04:00:00';
$date_2 = '2013-02-03 07:30:00';

echo myTimeDiff($date_1, $date_2);    // --> 2 Days 3 Hours

 

What result are you getting and what result is required?

If you want to specify the timezone of the dates: You'll need to create a DateTimeZone object, of the desired timezone, then send it as the second parameter when creating the DateTime object.

 

If you want to retrieve the timezone, and its offset (compared to GMT), then you have the "e" and "O" flags for the DateTime::format () method.

 

You can also, if I'm not completely mistaken, change the timezone and automatically adjust the date/time of the object. To do this, just set the timezone using the proper method for the DateTime object.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.