Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ilex01's avatar

strtotime('+2 hours') doens't work

<?php
$date = date_create($add->created_at);

$dateFr = date_format($date,"d/m/Y H:i:s");

$new_time = date($dateFr, strtotime('+2 hours'));

echo $new_time; // does not output the hour with +2 hours
?>
0 likes
5 replies
ilex01's avatar

@Tray2 Thank you for trying to help me but could you help me with my code?

JussiMannisto's avatar
Level 50

The first parameter of the date() function is the date format (e.g. 'Y-m-d'), but you're passing a formatted date string instead.

Here's one way to do it using strtotime():

$datetime = date_create($add->created_at);
$new_time = date('d/m/Y H:i:s', strtotime($datetime->format('c').' +2 hours'));

But since you're using a DateTime object, you can just do this:

$new_time = date_create($add->created_at)->modify('+2 hours')->format('d/m/Y H:i:s');
1 like

Please or to participate in this conversation.