1

Want to add hours in a date . Please help.

The PHP code is:

$createdDate=date_create("2013-03-17 07:11:00");
$hour = 4;

I tried using strtotime. But it gives an error.

Thanks

sm hz
  • 51
  • 1
  • 2
  • 9
  • You can use following. You want to add only 1 hour, There for you have to add 60*60*1 into current date; $date = date('h:i:s A', strtotime(date("Y-m-d H:i:s"))+3600); You get my Point? – chintan Mar 29 '13 at 09:58

3 Answers3

8

Use DateTime modify method.

$date = new DateTime("2013-03-17 07:11:00");
$date->modify("+4 hours");
echo $date->format("Y-m-d H:i:s");

DEMO.

Rikesh
  • 26,156
  • 14
  • 79
  • 87
3

You could use DateInterval:

$iv = new DateInterval("PT{$hour}H");
$createdDate->add($iv);
// $createdDate is now modified
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

You can use date_modify:

date_modify($createdDate, "+4 hours");
Sjoerd
  • 74,049
  • 16
  • 131
  • 175