user3503758
user3503758

Reputation: 77

What is the best way to add days to the date in PHP?

I have a date value with time. (27/4/2014 18:00:00)

In addition to it, I have a string that consists of days/hours/mins. (10/7/0).

In the end, I need to sum the two values making one date.

In this example, the sum of the values is 37/4/2014 28:7:00.

So the desired result would be 8/5/2014 4:7:00. What is the best way to get this result in PHP??

Upvotes: 0

Views: 70

Answers (2)

Parag Tyagi
Parag Tyagi

Reputation: 8960

function($date, $addon)
{
    $date1 = strtotime($date);

    $explode = explode('/', $addon);
    $date2 = ($explode[0]*86400) + ($explode[1]*3600) + ($explode[2]*60);

    return date('j/n/Y H:i:s', ($date1+$date2));
}

Upvotes: 1

thexacre
thexacre

Reputation: 692

I'd convert them to timestamps then add them and convert them back:

$time = strtotime($time1) + strtotime($time2);

echo date('m/d/Y h:i:s a', $time);

Upvotes: 0

Related Questions