Adding minutes to date time in PHP

PhpDateTime

Php Problem Overview


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?

Php Solutions


Solution 1 - 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.

Solution 2 - Php

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

Solution 3 - Php

$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 :-)

Solution 4 - Php

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.

Solution 5 - Php

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 . "<br>";

//adding 2 minutes
$convertedTime = date('Y-m-d H:i:s', strtotime('+2 minutes', strtotime($startTime)));

//display the converted time
echo '> '.$convertedTime;

Solution 6 - Php

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.

Solution 7 - Php

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

Solution 8 - Php

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

Solution 9 - Php

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

Solution 10 - Php

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

Solution 11 - Php

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 '<h1>Adjusted Time: ' . $adjustedTime . '</h1>' . PHP_EOL . PHP_EOL;

Solution 12 - Php

one line mysql datetime format

$mysql_date_time = (new DateTime())->modify('+15 minutes')->format("Y-m-d H:i:s");

Solution 13 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLuke BView Question on Stackoverflow
Solution 1 - PhpTim CooperView Answer on Stackoverflow
Solution 2 - PhpDanielView Answer on Stackoverflow
Solution 3 - PhpNemodenView Answer on Stackoverflow
Solution 4 - PhpBradView Answer on Stackoverflow
Solution 5 - Phpuser3361395View Answer on Stackoverflow
Solution 6 - PhpacaritoView Answer on Stackoverflow
Solution 7 - PhpPeterView Answer on Stackoverflow
Solution 8 - PhpBeaudinn GreveView Answer on Stackoverflow
Solution 9 - PhpDeathRsView Answer on Stackoverflow
Solution 10 - PhpRobert SchwindamanView Answer on Stackoverflow
Solution 11 - PhpFirstFraktalView Answer on Stackoverflow
Solution 12 - PhpSjaak WishView Answer on Stackoverflow
Solution 13 - PhpWajid khanView Answer on Stackoverflow