Possible Duplicates:
Number of seconds from now() to Sunday midnight
Calculates difference between two dates in PHP
Hello All,
In my project, I need to calculate the difference in seconds between two dates:
For Example :
$firstDay = "2011-05-12 18:20:20"; $secondDay = "2011-05-13 18:20:20";
Then I should get 86400 Seconds That is 24 hours.
Similarly For
$firstDay = "2011-05-13 11:59:20"; $secondDay = "2011-05-13 12:00:20";
It should return 60 Seconds.
I read lots of questions in the stackoverflow But they only deals
with the difference between 2 minute fields like 11:50:01 and 12:10:57
We have the startDate and endDate which we can convert both to timestamps in milliseconds with getTime . Then we subtract the timestamp of endDate by the timestamp of startDate . This will return the timestamp difference in milliseconds. So we divide the difference by 1000 to get the difference in seconds.
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.
To calculate the difference between the arrival and the departure in T-SQL, use the DATEDIFF(datepart, startdate, enddate) function. The datepart argument can be microsecond , second , minute , hour , day , week , month , quarter , or year . Here, you'd like to get the difference in seconds, so choose second.
Time Difference between two timestamps in Python Next, use the fromtimestamp() method to convert both start and end timestamps to datetime objects. We convert these timestamps to datetime because we want to subtract one timestamp from another. Next, use the total_seconds() method to get the difference in seconds.
$timeFirst = strtotime('2011-05-12 18:20:20'); $timeSecond = strtotime('2011-05-13 18:20:20'); $differenceInSeconds = $timeSecond - $timeFirst;
You will then be able to use the seconds to find minutes, hours, days, etc.
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