I have two times - For eg- the current time - 08:24 and date is 02/01/2013 in dd/mm/yyyy format and I have another time at 13:46 and date is 31/12/2012 . So, how can I calculate the difference between the 2 times in hours using PHP. (i.e. 42.63 hours) Thanks in advance.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
Convert them both to timestamp values, and then subtract to get the difference in seconds.
I think the following code is useful to get an idea about how to calculate time difference using PHP
The above function is useful to calculate difference between two times as well as dates. The dates are given as arguments with the output format.
The output format are given below:
// '%y Year %m Month %d Day %h Hours %i Minute %s Seconds' => 1 Year 3 Month 14 Day 11 Hours 49 Minute 36 Seconds // '%y Year %m Month %d Day' => 1 Year 3 Month 14 Days // '%m Month %d Day' => 3 Month 14 Day // '%d Day %h Hours' => 14 Day 11 Hours // '%d Day' => 14 Days // '%h Hours %i Minute %s Seconds' => 11 Hours 49 Minute 36 Seconds // '%i Minute %s Seconds' => 49 Minute 36 Seconds // '%h Hours => 11 Hours // '%a Days
Just putting this here, for anyone who needs to find the difference between two dates/timestamps in
Hours
,Minutes
'AND'Seconds
!!Another way is to use PHP's date-related classes. The example below uses
DateTime::diff()
to get aDateInterval
object ($interval
). It then uses the interval's properties to arrive at the total number of hours in the interval.I got a simple solution, Try this one -
If you have the dates as timestamps (use
strtotime
if needed), then just subtract them, optionally take the absolute value, then divide to 3600 (number of seconds in an hour). Easy ^_^