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 Show how to calculate the difference between 2 timestamps in php starttime:2016-11-30 03:55:06 endtimetime: 2016-11-30 11:55:06 asked Dec 1, 2016 at 7:46
3 Any procedural way should be avoided. Use OOP method for date time difference:
You can setup difference format as per your needs.
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 If you want to have total difference in seconds then you can use:
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 answered Dec 1, 2016 at 8:02
Abhay MauryaAbhay Maurya 11k6 gold badges44 silver badges60 bronze badges 2 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:
answered Dec 1, 2016 at 8:01
bio_spritebio_sprite 4282 silver badges14 bronze badges The simplest answer(for me of course) is here
In this case date_create() function creates the DateTime object answered Mar 23, 2018 at 17:34
AlexKhAlexKh 5362 gold badges8 silver badges18 bronze badges I have created one function which helps you to get Hours, Minutes and Days from the two Unix timestamps by just passing type as 3rd parameter.
answered Jul 6, 2020 at 14:22
0 @atul-baldaniya, I've modified your solution to avoid negative values and return integer results instead of decimal values.
answered May 21, 2021 at 22:52
If you need the difference in 00:00:00 (Hours, Minutes, Seconds) format, you can use this function. I used
answered Oct 8, 2021 at 11:03
try this code, tested on phpfiddle.org :-
answered Apr 18, 2018 at 13:53
How can I get the difference between two timestamps in php?How to calculate the difference between two dates in PHP ?. PHP | date_diff() Function.. Program to find the number of days between two dates in PHP.. Return all dates between two dates in an array in PHP.. PHP | fopen( ) (Function open file or URL). PHP | fread( ) Function.. PHP | fclose( ) Function.. PHP | fwrite( ) Function.. How do you find the difference between two timestamps?The difference is calculated by subtracting the second operand from the first. The result is rounded down, with any remainder discarded. For example, 61 minutes is equal to 1 hour, and 59 minutes is equal to 0 hours.
How can I find the difference between two timestamps in SQL?{fn TIMESTAMPDIFF(interval,startDate,endDate)} returns the difference between the starting and ending timestamps (startDate minus endDate) for the specified date part interval (seconds, days, weeks, and so on). The function returns an INTEGER value representing the number of intervals between the two timestamps.
How can I calculate hours between two dates in php?$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.
|