• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2182
  • Last Modified:

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?
0
skylabel
Asked:
skylabel
  • 4
  • 2
  • 2
  • +1
2 Solutions
 
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
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
what about this:
http://www.php.net/manual/en/function.date-modify.php

$commenceDateTime = date_modify($commenceDateTime, "+15 minute");
0
 
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 ....
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
skylabelAuthor Commented:
I'm using PHP 4. Noticed it's a PHP5 function
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
then even simpler:
$endDate = strtotime("+15 minute", $row['commenceDate']);

0
 
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
0
 
RoonaanCommented:
Hello skylabel,

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

Regards,

Roonaan
0
 
ygouthamCommented:
$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

0
 
ygouthamCommented:
sorry youhave to echo the $outputdate variable.  

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

as the line in the last post
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now