Luke B Luke B - 1 year ago 89
PHP Question

Adding minutes to date time in PHP

I'm really stuck with adding X minutes to a datetime, after doing lots of google'ing and PHP manual reading, I don't seem to be getting anywhere.

The date time format I have is:

2011-11-17 05:05
year-month-day hour:minute

Minutes to add will just be a number between 0 and 59

I would like the output to be the same as the input format with the minutes added.

Could someone give me a working code example, as my attempts don't seem to be getting me anywhere?

Answer Source
$minutes_to_add = 5;

$time = new DateTime('2011-11-17 05:05');
$time->add(new DateInterval('PT' . $minutes_to_add . 'M'));

$stamp = $time->format('Y-m-d H:i');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download