1

I've been trying to add 3 hours to a date set by a server. In the below example, I have filled out the $date_time and $id variables with pre-set content to avoid going into server details.

$date_time = "2015-10-21 21:01:00";

$id = 10;

$new_date = $date_time->modify("+3 hours"); 

$db->query("UPDATE prizes SET date='$new_date' WHERE id='$id'"); 

Why is this not working? This code is for a content site that adds 3 hours to it's countdown once someone wins.

UPDATE: Inserting variable into hours section I've tried this: $new_date = $date_time->modify("+{$hours} hours");

What am I doing wrong?

  • 1
    `$date_time` is a `"string"`. What you want is [`DateTime`](http://php.net/manual/en/class.datetime.php) – Darren Oct 23 '15 at 01:03
  • 1
    instantiate a datetime object and feed that `$date_time` string into the constructor – Kevin Oct 23 '15 at 01:09
  • Thanks Fred-ii. The answer you linked me to was perfect! Would you mind posting that as an answer and I will mark it as the accepted answer! :) –  Oct 23 '15 at 01:27
  • You're welcome Matthew. Thanks for the offer, but it was a duplicate and I can't post an answer, *cheers*. You can mark the one below that was given if you wish. – Funk Forty Niner Oct 23 '15 at 01:36
  • I'm trying to insert a variable into the hours section. This is what I've tried: $new_date = $date_time->modify("+{$hours} hours"); First person to figure this out will get the accepted answer... –  Oct 23 '15 at 01:40
  • I don't understand your new question. You said the link I posted was perfect. That would fall as a new question. – Funk Forty Niner Oct 23 '15 at 01:44
  • Your right! I found the answer to my question. To insert a variable, you do this: $new_time = date('Y-m-d H:i:s', strtotime("+$hours hours")); –  Oct 23 '15 at 01:47

1 Answers1

0

PHP doesn't know that your string is a date.

$date_time = date('Y-m-d h:i:s', strtotime("2015-10-21 21:01:00"));
Andrew
  • 4,443
  • 5
  • 33
  • 75