mostafatalebi Posted January 26, 2013 Share Posted January 26, 2013 Hello everybody I need a function in my site that calculates the period between two dates. How shall I approach this? $date_1 - $date_2 = $period I want to be able to get a time in clean appearance. Such as: 2 Days and 3 Hours Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/273660-getting-time-difference/ Share on other sites More sharing options...
Barand Posted January 26, 2013 Share Posted January 26, 2013 Check out this recent reply of mine http://forums.phpfreaks.com/topic/273650-adding-numbers-to-a-database-over-time/?do=findComment&comment=1408365 Quote Link to comment https://forums.phpfreaks.com/topic/273660-getting-time-difference/#findComment-1408368 Share on other sites More sharing options...
mostafatalebi Posted February 3, 2013 Author Share Posted February 3, 2013 Sorry but is there a way to GMT Offset the time object in your function? Quote Link to comment https://forums.phpfreaks.com/topic/273660-getting-time-difference/#findComment-1409915 Share on other sites More sharing options...
Barand Posted February 3, 2013 Share Posted February 3, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/273660-getting-time-difference/#findComment-1409925 Share on other sites More sharing options...
Christian F. Posted February 3, 2013 Share Posted February 3, 2013 (edited) 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. Edited February 3, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/273660-getting-time-difference/#findComment-1409926 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.