14

I want to add time to an existing date. I have 2 string variables:

$date     = "2013-01-05 10:55:15";
$interval =            "50:25:10";

I want to calculate the final date "2013-01-07 13:20:25". The hours in time can be bigger than 23, meaning that this interval can be greater than a whole day.

What's the best way to do this ?

Sammitch
  • 30,782
  • 7
  • 50
  • 77
Nelson Teixeira
  • 6,297
  • 5
  • 36
  • 73

5 Answers5

17

Use DateTime API:

$date = new DateTime("2013-01-05 10:55:15");
$date->add(new DateInterval("PT50H25M10S"));

then you can convert it back to string with the same date format you would use with date() function, if you want to:

$string = $date->format("Y-m-d H:i:s");

For more information about the DateInterval definition, visit this page:

DateInterval

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.

Here are some simple examples. Two days is P2D. Two seconds is PT2S. Six years and five minutes is P6YT5M.

so in this case PT50H25M10S means 50 hours, 25 minutes, and 10 seconds

Note that DateInterval is available only since PHP 5.3, if you have to use lower version, you could use something like this:

 $time = strtotime("2013-01-05 10:55:15");
 $time += 55*60*60 + 25*60 + 10;
 $newDate = date("Y-m-d H:i:s");
Adam Zielinski
  • 2,774
  • 1
  • 25
  • 36
  • *"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."* <- That's a snippet is from [this answer](https://stackoverflow.com/a/8169189/3965565). Combined with the above, these helped me finally understand DateInterval. – elbowlobstercowstand May 25 '23 at 07:07
6

This is a little tricky.

Normally what you would do here if it was a static period, or was a single period type, is something along the lines of:

$date = "2013-01-05 10:55:15";
$time = new DateTime($date);
$time->add(new DateInterval('PT5M'));

This would add 5 minutes to the datetime. However I doubt you can pass the whole interval in. So what you'll probably have to do is split the interval by : and then add each part of the interval (I assume it is dynamic?) to the date separately. So first hours, then minutes, then seconds

For more on this, see here: http://www.php.net/manual/en/datetime.add.php

Ricky S
  • 371
  • 1
  • 8
6

You could first explode the interval and then get the hours, minutes, seconds, and then use DateTime's add() to add the interval, like so:

$interval = '50:25:10';
$datestring = '2013-01-05 10:55:15';    
list($hours, $minutes, $seconds) = explode(':', $interval); 

$date = new DateTime($datestring);    
$date->add(new DateInterval('PT'.$hours.'H'.$minutes.'M'.$seconds.'S'));
echo $date->format('Y-m-d H:i:s');

Demo!

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
5

Use a DateInterval and DateTime->add():

$date = new DateTime("2013-01-05 10:55:15");
$date->add(new DateInterval("PT50H25M10S"));
Sammitch
  • 30,782
  • 7
  • 50
  • 77
1

In case anyone needs based on answers above, I made my function like this:

public function addTime($date, $time) {
    $atime = explode(":", $time);
    $_date = date_create_from_format("Y-m-d H:i:s", $date);
    $_date->add(new DateInterval("PT" . intval($atime[0])."H".intval($atime[1])."M".intval($atime[2])."S"));
    return $_date->format("Y-m-d H:i:s");
}

hope it helps someone

Nelson Teixeira
  • 6,297
  • 5
  • 36
  • 73