Adding minutes to a datetime

I've got a datetime field in MySQL in which I store a datetime. The field name is commenceDate. To read into PHP this I use the strtotime function :

$commenceDateTime = strtotime( $commenceDate );

Now I want to add an integer to $commenceDate, represented by the variable $duration (in minutes) so I can calculate the $endDateTime. How do I do this?
Who is Participating?
ygouthamConnect With a Mentor Commented:
$commenceDate = strtotime( $commenceDate );
$minutestoadd = 15;

$newtime = $commenceDate + ($minutestoadd * 60); //multiply by 60 for number of seconds in minutes
$outputdate = date("m-d-Y H:i", $newtime);

echo "$newtime is the incremented time<br>";

note that you just have to add the time in seconds once you have converted a date into string using strtotime() function in php

skylabelAuthor Commented:
And of course, I would like the output to be displayed as ("m-d-Y H:i")
i.e., month-date-year Hour:Minute
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
what about this:

$commenceDateTime = date_modify($commenceDateTime, "+15 minute");
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

skylabelAuthor Commented:
My code is:
$commenceDate = $row['commenceDate'];
$commenceDate = strtotime( $commenceDate );

$endDate = date_modify($commenceDate, "+15 minute");
echo date("m-d-Y H:i", $endDate);

I get an error:
date_modify() expects parameter 1 to be DateTime, integer given in ....
skylabelAuthor Commented:
I'm using PHP 4. Noticed it's a PHP5 function
Guy Hengel [angelIII / a3]Billing EngineerCommented:
then even simpler:
$endDate = strtotime("+15 minute", $row['commenceDate']);

skylabelAuthor Commented:
No error but I dunno why but the dates coming up are wrong

$commenceDate          $endDate
10-25-2007 04:30      01-01-1970 00:48      
10-13-2007 05:30      01-01-1970 00:48      
10-11-2007 05:30      01-01-1970 00:48

Output always is:
01-01-1970 00:48
Hello skylabel,

You would use $endDate = strtotime("+15 minute", $commenceDateTime);


sorry youhave to echo the $outputdate variable.  

echo "$outputdate is the incremented time in new format <br>";

as the line in the last post
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.