Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between 2 dates in seconds [duplicate]

Tags:

date

php

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

like image 358
Pushpendra Avatar asked May 13 '11 07:05

Pushpendra


People also ask

How do you find the difference between two dates and seconds?

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.

How do you find the time difference between two timestamps 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.

How do I find the difference between two dates and seconds in SQL?

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.

How do you find the difference in seconds in Python?

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.


1 Answers

$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.

like image 167
Jordonias Avatar answered Oct 10 '22 23:10

Jordonias