Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - add two hours to date variable

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 btw)

$date = 2011-10-18T19:56:00+0200

I converted that string into date using this command:

$time = date_format(DateTime::createFromFormat("Y-m-d\TH:i:sO", $date), "G:i")

Now, I'd like to add 3 minutes to that variable, but I I'm not sure how. I've used the following command in my script before, but that applies to the current date/time, so I'm not sure how to use that for my time variable:

$currenttime = date('G:i', strtotime('+2 hours'));

So, how can I add three minutes to the $time variable?

like image 854
laarsk Avatar asked Nov 30 '22 16:11

laarsk


2 Answers

echo $idate="2013-09-25 09:29:44";

$effectiveDate = strtotime("+40 minutes", strtotime($idate));

echo date("Y-m-d h:i:s",$effectiveDate);
like image 187
saikat Avatar answered Dec 10 '22 07:12

saikat


Since you're using the DateTime object already, stick with it:

$time = DateTime::createFromFormat("Y-m-d\TH:i:sO", $date);
$three_minutes = $time->add(new DateInterval('P2H'));
                                               ^^--two (2) hours (H)
like image 41
Marc B Avatar answered Dec 10 '22 09:12

Marc B