I need to subtract 45 minutes from the date-time variable in PHP.
The code:
$thestime = '2012-07-27 20:40';
$datetime_from = date("Y-m-d h:i",strtotime("-45 minutes",strtotime($thestime)));
echo $datetime_from;
returns the result 2012-07-27 07:55.
It should be 2012-07-27 19:55, though. How do I fix this?
You should do:
Having
Hinstead ofhmeans a 24-hour format is used, representing the hour with leading zeros:00through23.You can read more on this in the PHP date function documentation.
There are also object oriented ways of doing this which are more fluent, like
DateTime::sub:Or the even more expressive way offered by the
Carbonlibrary which extends PHP’s built inDateTimeclass: