I want to add 3 minutes to a date/time variable I have, but I\'m not sure how to do this. I made the variable from a string like this: (which is in the RFC 2822 date format
Instead of
$time = DateTime::createFromFormat("Y-m-d\TH:i:sO", $date);
$time = $time->add(new DateInterval('P2H'));
try (for adding 3 minutes)
$time = DateTime::createFromFormat("Y-m-d\TH:i:sO", $date);
$time->add(new DateInterval('PT3M'));
First, since you're using PHP's DateTime class, you don't need to assign the output of the add
method to a variable - it will modify the DateTime you passed into the constructor. Second, if you're making modifications to time using the same class, you have to make sure there's a T
before your time definition. For your example, DateInterval('P2H')
is invalid - it should be DateInterval('PT2H')
.
You could just use strtotime
twice:
$date = strtotime('2011-10-18T19:56:00+0200');
echo date('G:i', strtotime('+3 minutes', $date));