I am using 2 timestamp in my table which is starttime datatype- timestamp and as current timestamp. endtime datatype-timestamp and default as 0000-00-00 00:00:00
how to calculate the difference between 2 timestamps in php starttime:2016-11-30 03:55:06 endtimetime: 2016-11-30 11:55:06
To calculate the difference between two dates in PHP, call date_diff() date/time function, and pass the two dates as argument to it. date_diff() function returns a DateInterval object, or FALSE if calculating the difference is not successful.
If you'd like to calculate the difference between the timestamps in seconds, multiply the decimal difference in days by the number of seconds in a day, which equals 24 * 60 * 60 = 86400 , or the product of the number of hours in a day, the number of minutes in an hour, and the number of seconds in a minute.
$date1 = "2007-03-24"; $date2 = "2009-06-26"; $diff = abs(strtotime($date2) - strtotime($date1)); $years = floor($diff / (365*60*60*24)); $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); printf("%d years, %d months, %d ...
$hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1); Dividing by 3600 because there are 3600 seconds in one hour and using round() to avoid having a lot of decimal places.
Any procedural way should be avoided. Use OOP method for date time difference:
$datetime1 = new DateTime('2016-11-30 03:55:06');//start time $datetime2 = new DateTime('2016-11-30 11:55:06');//end time $interval = $datetime1->diff($datetime2); echo $interval->format('%Y years %m months %d days %H hours %i minutes %s seconds');//00 years 0 months 0 days 08 hours 0 minutes 0 seconds
You can setup difference format as per your needs.
%Y - use for difference in year %m - use for difference in months %d - use for difference in days %H - use for difference in hours %i - use for difference in minutes %s - use for difference in seconds
You can remove any of above values as per your need. For example if you only are interested in hour difference and you know that difference cant be more than 24 hours then use only %H
.
If you want to have total difference in seconds then you can use:
echo $difference_in_seconds = strtotime('2016-11-30 11:55:06') - strtotime('2016-11-30 03:55:06');//28800
Depends upon your need and the final format in which you want to have time difference.
For reference check: http://php.net/manual/en/datetime.diff.php
I hope it helps
You can convert your timestamps to unix timestamp (time in seconds) using php strtotime and then take the difference. You now have the difference in time in seconds and can convert to what you need...hours, min, days
http://php.net/manual/en/function.strtotime.php
ex:
$ts1 = strtotime($start); $ts2 = strtotime($end); $seconds_diff = $ts2 - $ts1; $time = ($seconds_diff/3600);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With