Adding Minutes to Date Time in PHP

Adding minutes to date time in PHP


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

The ISO 8601 standard for duration is a string in the form of P{y}Y{m1}M{d}DT{h}H{m2}M{s}S where the {*} parts are replaced by a number value indicating how long the duration is.

For example, P1Y2DT5S means 1 year, 2 days, and 5 seconds.

In the example above, we are providing PT5M (or 5 minutes) to the DateInterval constructor.

PHP Adding 15 minutes to Time value

Your code doesn't work (parse) because you have an extra ) at the end that causes a Parse Error. Count, you have 2 ( and 3 ). It would work fine if you fix that, but strtotime() returns a timestamp, so to get a human readable time use date().

$selectedTime = "9:15:00";
$endTime = strtotime("+15 minutes", strtotime($selectedTime));
echo date('h:i:s', $endTime);

Get an editor that will syntax highlight and show unmatched parentheses, braces, etc.

To just do straight time without any TZ or DST and add 15 minutes (read zerkms comment):

 $endTime = strtotime($selectedTime) + 900;  //900 = 15 min X 60 sec

Still, the ) is the main issue here.

adding 30 minutes to date

Your method of using strtotime should work.

<?php

echo date("Y/m/d H:i:s", strtotime("now")) . "\n";
echo date("Y/m/d H:i:s", strtotime("+30 minutes"));

?>

Output

2012/03/22 10:55:45
2012/03/22 11:25:45 // 30 minutes later

However your method of adding time probably isn't correct. The above will work to add 30 minutes to the current time. Suppose you want to add 30 minutes from a given time, $t, then use strtotime's second parameter, which is used as a base for the calculation of relative dates.

date("Y/m/d H:i:s", strtotime("+30 minutes", $t));

http://codepad.org/Z5yquF55

Add time to date time in php

You can try with strtotime and date function

$dateTime = '2016-09-08 21:00';
echo date( "Y-m-d h:i", strtotime( "2016-09-08 21:00 +4 hours" ) );
echo date( "Y-m-d H:i", strtotime( "2016-09-08 21:00 +1 hours 40 minutes" ) );

Adding minutes to time in PHP

I replaced your variable names with shorter ones, but this code works:

$from = '09:00:00'; //starting string
$duration = 90; //in minutes
$date = new DateTime($from);
$date->add(new DateInterval("PT{$duration}M"));//add minutes
$to = $date->format('H:i:s'); // '10:30:00'

Live demo

PHP Date Time Current Time Add Minutes

I think one of the best solutions and easiest is:

date("Y-m-d", strtotime("+30 minutes"))

Maybe it's not the most efficient but is one of the more understandable.

How to add 5 minutes to current datetime on php 5.3


$date = '2011-04-8 08:29:49';
$currentDate = strtotime($date);
$futureDate = $currentDate+(60*5);
$formatDate = date("Y-m-d H:i:s", $futureDate);

Now, the result is 2011-04-08 08:34:49 and is stored inside $formatDate

Enjoy! :)

Add 'x' number of hours to date

You may use something like the strtotime() function to add something to the current timestamp. $new_time = date("Y-m-d H:i:s", strtotime('+5 hours')).

If you need variables in the function, you must use double quotes then like strtotime("+{$hours} hours"), however better you use strtotime(sprintf("+%d hours", $hours)) then.



Related Topics



Leave a reply



Submit