Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of Minutes between two dates

Tags:

html

php

say i have a date which is

$future_time_ending = "2012-09-21 12:12:22"

How do i work out the number of minutes between the current time and the $future_time_ending?

Thanks

like image 417
Al Hennessey Avatar asked Sep 20 '12 20:09

Al Hennessey


People also ask

How do I calculate the number of minutes between two dates?

To get the number of minutes between 2 dates: Get the number of milliseconds between the unix epoch and the Dates. Subtract the milliseconds of the start date from the milliseconds of the end date. Divide the result by the number of milliseconds in a minute - 60 * 1000 .

How do I calculate minutes between dates in Excel?

(END TIME - START TIME)*1440 We subtract time/dates in excel to get the number of days. Since a day has 1440 (24*60) minutes, we multiply the result by 1440 to get the exact number of minutes.

Can you calculate the time between two dates?

To calculate the number of days between two dates, you need to subtract the start date from the end date. If this crosses several years, you should calculate the number of full years. For the period left over, work out the number of months.


1 Answers

One method:

$minutes = (strtotime("2012-09-21 12:12:22") - time()) / 60;

strtotime converts the date to a Unix timestamp - the number of seconds since the Unix epoch. Subtract the current timestamp and you have the number of seconds between the current time and the future time. Divide by 60 and the result is in minutes.

If you don't know for certain the time you're comparing is in the future, take the absolute value to get a positive number:

$minutes = abs(strtotime("2012-09-21 12:12:22") - time()) / 60;

Just to be complete in my answer, there is a more elaborate OO approach available in PHP:

$time = new DateTime("2012-09-21 12:12:22");
$diff = $time->diff(new DateTime());
$minutes = ($diff->days * 24 * 60) +
           ($diff->h * 60) + $diff->i;

This is especially useful if the input time is from a time zone other than the server's.

like image 144
Matt S Avatar answered Sep 29 '22 14:09

Matt S