View Full Version : php date time functions
11-24-2010, 04:11 PM
I have the following date time format:
I want to get a beginning datetime and an ending datetime and caluculate the difference in hours. How can i transform the above date to a format that can caluslte? the stamp posted is from a calendar date picker.
thanks in advance
11-24-2010, 04:20 PM
Change it to unix timestamp ....
The timestamp resolution is in seconds.
11-24-2010, 04:38 PM
and the above format is acceptable so i dont have to change it?
11-24-2010, 05:52 PM
Correct ... your format is fine.
The conversion (strtotime) is only for calculation.
Like this example:
// difference in seconds
// difference in hours (rounded up).
01-25-2013, 10:16 AM
Good solution above....These post help me lot...........Thanks
01-25-2013, 02:52 PM
I'm a fan of the dateinterval myself. Hours is a pain though, so for that I'd still suggest sticking with strtotime (or massage the DateInterval which would be more work). It's a bit easier to use overall IMO.
$time1= new DateTime("24-11-2010 11:6:57");
$time2= new DateTime("24-11-2010 19:4:35");
$diDiff = $time1->diff($time2);
$diDiff is the daterInterval difference between the two. Best used for large dates, not small (it doesn't keep track of a total number of hours, only a total number of days).
print('Difference is ' . $diDiff->format('%y years, %m months, %d days, %h hours (total days: %a)'));
The diff on dateTime also has an inversion parameter to always force absolute.
So with the smaller times, I would recommend you use simple subtraction (either using strtotime to get the integer, or by using DateTime and pulling the integer out) as indicated here. For anything larger, I'd suggest the DateInterval (to see how many years, months, days, etc ago). The DateInterval doesn't play great with hours, but can be easily calculated based on the total number of days past.
And yes, I'm also aware that this thread is a couple of years old now, so the DateInterval would just *barely* have applied as it was released mid 2009.
01-25-2013, 11:04 PM
I'm pretty much blown away at how much trouble PHP has at doing date functions. There are dozens of date functions to pick from, 'use this one', 'use that one', 'don't use that unless you do this', etc. Make sure your dates are formatted like YYYY-MM-DD for more accurate calculations, etc. How does anyone know which method to use and when to use it?
What's happening? Does Perl, Java, C++, and all the others have the same issues with date/time calculations? Or is this something where PHP did it wrong from the beginning and now it has escalated into a whole fiasco of functions and tricks to make it work?
01-26-2013, 05:19 AM
IMO its something that the older PHP versions did wrong. Or its possible that there was no easy way to create backwards compatibility with the existing procedural code and so there was no option but to write another set of functions to handle them. Both are good possibilities.
DateTime is 5.x functionality. The entire library is unstable between 5.0 and 5.3 where many new classes and methods were added. So the only frustrating thing is writing something on 5.3 which just doesn't work with say 5.2.8.
strtotime is also instable though as indicated by the changelogs. <4.4, next was listed as +2, <5.0.2 now was incorrectly calculated from midnight, and < 5.1.0 returns -1 on failure instead of false which is just silly since -1 is a valid time.
Compared to other languages, its hard to say. Most other languages I use you import to do many of the time manipulations. Before namespace (and even since due to backwards compatibility), php is stuck with only global symbols so it has dozens of functions for date and time.
02-21-2013, 11:21 AM
Your mentioned date & time format is perfect. You can use this format any time.
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.