10

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

2
  • This is a duplicate of this post which has a good answer.
    – mbfisher
    Sep 20, 2012 at 20:12
  • What have you tried so far? Where are you stuck?
    – Nico Haase
    May 8, 2021 at 8:43

1 Answer 1

36

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.

2
  • ok great, what does the abs actually do though? I have never seen that before Sep 20, 2012 at 20:17
  • 2
    FINALLY an answer I can actually use! I've tried probably like 10 different methods but none of them worked the way I wanted
    – K. P.
    Oct 30, 2019 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.