128

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?

14 Answers 14

233
$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.

10
  • With date: 2011-11-18 00:00 if I add 5 mins, I get 2012-04-18 00:00 as a result. ` $time = new DateTime($_REQUEST['start']); $time->add(new DateInterval('P' . $duration . 'M')); $endTime = $time->format('Y-m-d H:i'); echo $endTime; ` Applogies for the formatting, it appears I can't work this out either today ^_^
    – Luke B
    Nov 17, 2011 at 15:12
  • 2
    How is DateTime better than strtotime?
    – bozdoz
    Nov 17, 2011 at 15:25
  • 13
    What does 'PT' represent? Jul 18, 2016 at 21:43
  • 1
    @bozdoz It's not a question of which method is "better". strtotime produces a Unix timestamp as an integer which is helpful if you are for example sorting dates in php. The DateTime class provides options for object and procedural style operations. It's more readable and also has better support for handling differences between different timezones; for example, if you have a project that outputs dates for numerous different timezones sourced from the same data instance.
    – DrewT
    Aug 17, 2016 at 19:54
  • 3
    @NicholasJohn16 php.net/manual/en/dateinterval.construct.php >> The format starts with the letter P, for "period." Each duration period is represented by an integer value followed by a period designator. If the duration contains time elements, that portion of the specification is preceded by the letter T.
    – vee
    Jul 5, 2019 at 10:22
151

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");
2
  • 4
    Both of the top answers are great and work fine, but I prefer this one. Just seems simpler and more intuitive.
    – HartleySan
    Feb 19, 2020 at 16:29
  • 1
    A benefit of this method is it can handle negative minutes (if you remove the +), unlike the highest score answer.
    – will
    Jan 14 at 4:36
55
$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 :-)

4
  • 2
    Most understanding one
    – Naveen DA
    Aug 11, 2017 at 5:16
  • Live demo is no longer available Aug 17, 2021 at 12:29
  • 1
    @HasanAlyazidi thank you, I've updated the reference.
    – Nemoden
    Aug 19, 2021 at 12:27
  • @Nemoden Nemoden Live demo link is broken, please remove it or update it. Thanks.
    – Kamlesh
    Dec 22, 2022 at 9:26
21

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.

10

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

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.

4

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

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

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

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

one line mysql datetime format

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

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

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");
2
  • 1
    What is $yourDate? What does this answer add to the similar answers posted years before?
    – None
    Jul 29, 2019 at 14:32
  • Dear @J.Money $yourDate means $variable name and Maybe my answer will be helpful for someone after 100 years. It does not matter when we should answer until question is not closed. I hope you will understand and remove negative marking.
    – Wajid khan
    Jul 30, 2019 at 19:18
-1

All that you need to do is:

1- put your time in the format you want

2- add minutes time in the string

3- use strtotime() function for converting string to time

Here is an example:

$limit = 10;
date("G:i", strtotime(date("h:i:sa") . ' + ' . $limit . ' minutes'))

First, we had our time date("h:i:sa") which we want to add minutes to it

Then, we concatinated a string of our value to be added to it which is "H-m-sa +10 minutes", just change th time and the value of $limit

After that, convert all of this to date type in date() function

Note, check first that $limit is an integer, sometimes you receive the value of $limit but not value.

I hope this will help you.

2
  • Are you sure there is no answer suggesting the same? Like this one? May 30, 2023 at 15:53
  • I faced the same problem and I answered my EXACT solution that I tried. May 31, 2023 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.