-3

The title says it all. I have a PHP variable that says: 07/05/2016.

I've tried strtodate, the date function, but nothing seems to be working. How can I now add one hour to this date?

3 Answers3

6

There's quite a few ways to do this with PHP. Here's one using DateTime():

$datetime = new DateTime('07/05/2016');
$datetime->modify('+1 hour');
echo $datetime->format('Y-m-d H:i:s');

In your case you can also just add the literal time since there is no time for that date and midnight is assumed:

echo '07/05/2016 01:00:00';

Just for fun, here are a few more ways to do it:

// Using DateInterval()
$datetime = new DateTime('07/05/2016');
$datetime->add(new DateInterval('PT1H'));
echo $datetime->format('Y-m-d H:i:s');

// As a one-liner
echo (new DateTime('07/05/2016'))->->modify('+1 hour')->format('Y-m-d H:i:s');
John Conde
  • 217,595
  • 99
  • 455
  • 496
3

You can do it in a simple way

$date = "2019-08-20 17:00:00";
echo date("Y-m-d H:i:s", strtotime("{$date} +1 hour"));
//result 2019-08-20 18:00:00
Dey
  • 842
  • 9
  • 21
0

Here, +1 hour will add one hour.

$date = 07/05/2016

echo date("Y-m-d", strtotime ($date,"+1 hour"));

And for time,

echo date("Y-m-d H:i:s", strtotime ($date,"+1 hour"));
Virb
  • 1,639
  • 1
  • 16
  • 25