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?
Tim Cooper
154k37 gold badges321 silver badges273 bronze badges
asked Nov 17, 2011 at 14:51
$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.
Daniel
7,7875 gold badges57 silver badges80 bronze badges
answered Nov 17, 2011 at 14:54
Tim CooperTim Cooper
154k37 gold badges321 silver badges273 bronze badges
10
PHP's DateTime class has a useful modify method which takes in easy-to-understand text.
$dateTime = new DateTime['2011-11-17 05:05'];
$dateTime->modify['+5 minutes'];
You could also use string interpolation or concatenation to parameterize it:
$dateTime = new DateTime['2011-11-17 05:05'];
$minutesToAdd = 5;
$dateTime->modify["+{$minutesToAdd} minutes"];
answered Sep 30, 2014 at 23:50
DanielDaniel
7,7875 gold badges57 silver badges80 bronze badges
1
$newtimestamp = strtotime['2011-11-17 05:05 + 16 minute'];
echo date['Y-m-d H:i:s', $newtimestamp];
result is
2011-11-17 05:21:00
Live demo is here
If you are no familiar with
strtotime
yet, you better head to php.net
to discover it's great power :-]
answered Nov 17, 2011 at 14:55
NemodenNemoden
8,5266 gold badges39 silver badges65 bronze badges
3
You can do this with native functions easily:
strtotime['+59 minutes', strtotime['2011-11-17 05:05']];
I'd recommend the DateTime class method though, just posted by Tim.
answered Nov 17, 2011 at 14:54
BradBrad
155k48 gold badges341 silver badges511 bronze badges
I don't know why the approach set as solution didn't work for me. So I'm posting here what worked for me in hope it can help anybody:
$startTime = date["Y-m-d H:i:s"];
//display the starting time
echo '> '.$startTime . "
";
//adding 2 minutes
$convertedTime = date['Y-m-d H:i:s', strtotime['+2 minutes', strtotime[$startTime]]];
//display the converted time
echo '> '.$convertedTime;
answered Jun 19, 2019 at 10:42
user3361395user3361395
1412 silver badges4 bronze badges
I thought this would help some when dealing with time zones too. My modified solution is based off of @Tim Cooper's solution, the correct answer above.
$minutes_to_add = 10;
$time = new DateTime[];
**$time->setTimezone[new DateTimeZone['America/Toronto']];**
$time->add[new DateInterval['PT' . $minutes_to_add . 'M']];
$timestamp = $time->format["Y/m/d G:i:s"];
The bold line, line 3, is the addition. I hope this helps some folks as well.
answered Sep 21, 2016 at 18:54
acaritoacarito
6247 silver badges15 bronze badges
A bit of a late answer, but the method I would use is:
// Create a new \DateTime instance
$date = DateTime::createFromFormat['Y-m-d H:i:s', '2015-10-26 10:00:00'];
// Modify the date
$date->modify['+5 minutes'];
// Output
echo $date->format['Y-m-d H:i:s'];
Or in PHP >= 5.4
echo [DateTime::createFromFormat['Y-m-d H:i:s', '2015-10-26 10:00:00']]->modify['+5 minutes']->format['Y-m-d H:i:s']
answered Oct 26, 2015 at 8:42
PeterPeter
8,3896 gold badges55 silver badges92 bronze badges
If you want to give a variable that contains the minutes.
Then I think this is a great way to achieve this.
$minutes = 10;
$maxAge = new DateTime['2011-11-17 05:05'];
$maxAge->modify["+{$minutes} minutes"];
answered Jan 16, 2015 at 16:27
Use strtotime["+5 minute", $date];
Example:
$date = "2017-06-16 08:40:00";
$date = strtotime[$date];
$date = strtotime["+5 minute", $date];
echo date['Y-m-d H:i:s', $date];
answered Jun 16, 2017 at 3:10
DeathRsDeathRs
1,06017 silver badges22 bronze badges
As noted by Brad and Nemoden in their answers above, strtotime[] is a great function. Personally, I found the standard DateTime Object to be overly complicated for many use cases. I just wanted to add 5 minutes to the current time, for example.
I wrote a function that returns a date as a string with some optional parameters:
1.] time:String | ex: "+5 minutes" [default = current time]
2.] format:String | ex: "Y-m-d H:i:s" [default = "Y-m-d H:i:s O"]
Obviously, this is not a fully featured method. Just a quick and simple function for modifying/formatting the current date.
function get_date[$time=null, $format='Y-m-d H:i:s O']
{
if[empty[$time]]return date[$format];
return date[$format, strtotime[$time]];
}
// Example #1: Return current date in default format
$date = get_date[];
// Example #2: Add 5 minutes to the current date
$date = get_date["+5 minutes"];
// Example #3: Subtract 30 days from the current date & format as 'Y-m-d H:i:s'
$date = get_date["-30 days", "Y-m-d H:i:s"];
answered Apr 30, 2016 at 19:01
One more example of a function to do this: [changing the time and interval formats however you like them according to this for function.date, and this for DateInterval]:
[I've also written an alternate form of the below function.]
// Return adjusted time.
function addMinutesToTime[ $dateTime, $plusMinutes ] {
$dateTime = DateTime::createFromFormat[ 'Y-m-d H:i', $dateTime ];
$dateTime->add[ new DateInterval[ 'PT' . [ [integer] $plusMinutes ] . 'M' ] ];
$newTime = $dateTime->format[ 'Y-m-d H:i' ];
return $newTime;
}
$adjustedTime = addMinutesToTime[ '2011-11-17 05:05', 59 ];
echo 'Adjusted Time: ' . $adjustedTime . '
' . PHP_EOL . PHP_EOL;
answered Apr 4, 2017 at 20:26
one line mysql datetime format
$mysql_date_time = [new DateTime[]]->modify['+15 minutes']->format["Y-m-d H:i:s"];
answered Dec 18, 2018 at 14:40
Sjaak WishSjaak Wish
3652 silver badges8 bronze badges
Without using a variable:
$yourDate->modify["15 minutes"];
echo $yourDate->format[ "Y-m-d H:i"];
With using a variable:
$interval= 15;
$yourDate->modify["+{$interval } minutes"];
echo $yourDate->format[ "Y-m-d H:i"];
answered Jan 13, 2018 at 12:04
Wajid khanWajid khan
7948 silver badges18 bronze badges
2