0

I've tried multiple examples/questions around and so far I cannot get this to work. i am trying to take the current DateTime, add 1 hour, then set the minutes to 30. Here is what I have:

Current Time:

$date = date('Y-m-d h:i:s'); //this works and echos properly

Add hour:

$date->add(new DateInterval("PT1H"));

I receive the following error

Call to a member function add() on a non-object

Then once that is done I'm planning to add:

$date->format('Y-m-d h:30:00'); //commented out on my script until I get the above piece working.
jpgerb
  • 1,043
  • 1
  • 9
  • 36

2 Answers2

2

date() does not produce a DateTime() object which is required to use DateTime::add(). Change:

$date = date('Y-m-d h:i:s'); 

to

$date = new DateTime();
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • I just realized my DateTime got switched to Date - not sure when that happened.. thanks for catching that - next problem - format to YYYY-MM-DD HH-MM-SS so I can get it in to my SQL DB.. any suggestions? – jpgerb Jul 07 '15 at 18:11
  • works great - thanks! I'll accept in 5 minutes when it lets me – jpgerb Jul 07 '15 at 18:15
1

Try doing:

$date = new DateTime('NOW');
$date->add(new DateInterval("PT1H"));
Mindastic
  • 4,023
  • 3
  • 19
  • 20