Pergunta

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?

Foi útil?

Solução

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

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top