Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - time minus time to minutes

Tags:

php

In php i have two times - 11:00:00 and 12:45:00. I want to get the difference between them in minutes, in this case 105 minutes. Whats the best way that can be done?

Thank you!

like image 745
user1985273 Avatar asked Mar 13 '13 17:03

user1985273


People also ask

How to subtract minutes from time in PHP?

To subtract 15 minutes you can do: date('Y-m-d H:i:s', (time() - 60 * 15)); In case you're looking to subtract seconds you can simply do: date('Y-m-d H:i:s', (time() - 10));

How to decrease time in PHP?

1 Answer. Show activity on this post. $newTime = strtotime("$now +3 hours +53 minutes"); echo "New time is: " . date("H:i:s", $newTime);


2 Answers

Here you go:

( strtotime('12:45:00') - strtotime('11:00:00') ) / 60

strtotime() is a very useful function. It returns the Unix timestamp for a wide variety of times and dates. So, if you take the two timestamps, and subtract them, then you have the difference in seconds. Divide by 60 to get the minutes.

like image 95
Jonathan Wren Avatar answered Oct 16 '22 09:10

Jonathan Wren


    $time_diff = strtotime('2013-03-13 12:45:00') - strtotime('2013-03-13 11:00:00');
    echo $time_diff/60;

I just kept dates as not sure if I keep the time part only it would return the correct diff or not.

EDIT

I just tested it works without date too ...

    $time_diff = strtotime('12:45:00') - strtotime('11:00:00');
    echo $time_diff/60;

So to answer you question - strtotime() returns a timestamp (the number of seconds since January 1 1970 00:00:00 UTC) so you simply divide it by 60 to convert it result into minutes.

like image 38
TigerTiger Avatar answered Oct 16 '22 09:10

TigerTiger