问题
I'm using date('H', strtotime($_GET['h'].' +1 hour')) at the moment ($_GET['h'] contains the hour I'm getting from the URL, i.e. 20 as in 20:00) but it only prints 01 when it should print 21. I want to get this hour and add one hour to it so 20 will be 21 and so on.
What am I doing wrong here?
回答1:
Something like this should work:
$time = DateTime::createFromFormat('H:i', '20:00');
$time->modify('+1 hour');
echo $time->format('H:i');
See it in action
回答2:
You can add seconds to the timestamp to do this:
date('H',mktime($_GET['h']) + 60 * 60)
Since the timestamp must be in seconds, the first 60 * 60 gets the hour in seconds. The second one adds an hour's worth of seconds to the timestamp.
来源:https://stackoverflow.com/questions/21998913/add-one-hour-from-get