Add one hour from _GET

China☆狼群 提交于 2019-12-11 18:14:38

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!