Nelson Teixeira
Nelson Teixeira

Reputation: 6562

What's the best way of adding a time interval in PHP?

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 ?

Upvotes: 14

Views: 24508

Answers (5)

Amal Murali
Amal Murali

Reputation: 76656

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!

Upvotes: 6

Nelson Teixeira
Nelson Teixeira

Reputation: 6562

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

Upvotes: 1

Adam Zielinski
Adam Zielinski

Reputation: 2874

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

Upvotes: 17

Ricky S
Ricky S

Reputation: 371

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

Upvotes: 6

Sammitch
Sammitch

Reputation: 32232

Use a DateInterval and DateTime->add():

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

Upvotes: 5

Related Questions