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!
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));
1 Answer. Show activity on this post. $newTime = strtotime("$now +3 hours +53 minutes"); echo "New time is: " . date("H:i:s", $newTime);
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.
$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.
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