0

I get how to use strtotime, but I first need to set a timezone, which is causing an issue:

$date = new DateTime();
$date->setTimezone(new DateTimeZone('America/New_York'));
$idate = $date->format('Y-m-d H:i:s'); 
$fdate=strtotime($idate,"+2 hours"); 

$idate comes out fine, in the correct timezone, in this format: 2016-07-25 15:56:24

How can I add 2 hours onto this and return a variable in the same format?

jonmrich
  • 4,233
  • 5
  • 42
  • 94
  • Possible duplicate of [Add 'x' amount of hours to date](http://stackoverflow.com/questions/11386308/add-x-amount-of-hours-to-date) – galeaspablo Jul 25 '16 at 20:01
  • Please look at http://stackoverflow.com/questions/11386308/add-x-amount-of-hours-to-date - it has several good answers. My favorite one is the OO answer. – galeaspablo Jul 25 '16 at 20:02
  • `$fdate = $date->modify('+2 hours');` – AbraCadaver Jul 25 '16 at 20:02
  • I suggest you take a look here: http://php.net/manual/en/datetime.add.php. Also, `strtotime` converts a human-readable string into a number representing the datetime, and it is completely independent of the class `DateTime`. – Nathan Robb Jul 25 '16 at 20:03

2 Answers2

1

Try this:

$fdate=date('Y-m-d H:i:s',strtotime($idate."+2 hours")); 
coderodour
  • 1,072
  • 8
  • 16
0

Using your $date, you can do:

$date = $date->add(new DateInterval('PT2H'));

and then format it as you like.

Matteo Tassinari
  • 18,121
  • 8
  • 60
  • 81