Link to home
Start Free TrialLog in
Avatar of skylabel
skylabel

asked on

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?
Avatar of skylabel
skylabel

ASKER

And of course, I would like the output to be displayed as ("m-d-Y H:i")
i.e., month-date-year Hour:Minute
SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ....
I'm using PHP 4. Noticed it's a PHP5 function
then even simpler:
$endDate = strtotime("+15 minute", $row['commenceDate']);

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

Regards,

Roonaan
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry youhave to echo the $outputdate variable.  

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

as the line in the last post