10

In my PHP application I want to calculate the sum of two time variables. I am looking for something like this example.

$time1 = 15:20:00;
$time2 = 00:30:00;
$time = $time1+$time2;
6
  • 4
    How have you tried to do this? Commented Jul 30, 2012 at 11:45
  • 1
    Do you have some examples of what your results should look like? Commented Jul 30, 2012 at 11:46
  • 1
    DateTime and DateInterval classes. php.net/manual/en/book.datetime.php Commented Jul 30, 2012 at 11:47
  • The result should added current time + a time variable which i am taking from DB say $tim2 Commented Jul 30, 2012 at 11:50
  • date and times cant be added directly, checkout Sammaye answer, you have to convert them to strings first and then add. Commented Sep 12, 2013 at 12:10

7 Answers 7

9

If the answer you expect is 15:50:00 and you want to use strtotime and date functions, you need to subtract the seconds $time1 and $time2 share when you transform them to unix timestamps:

$time1 = '15:20:00';
$time2 = '00:30:00';
$time = strtotime($time1) + strtotime($time2) - strtotime('00:00:00');
$time = date('H:i:s', $time);
Sign up to request clarification or add additional context in comments.

1 Comment

This is great and simple but it would be more relevant if you can provide further explanations about subtracting that 0 value time.
5

The best way to do this is most likely to use strtotime to convert them to timestamps and then do the adding together:

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

If I remember right strtotime does support this format.

Otherwise you will need to filter it out yourself.

7 Comments

The result should added current time + a time variable which i am taking from DB say $tim2
$time = date("H:i:s")+strtotime($mysql_time); you mean this way..?
@Rakesh No, time() returns a timestamp of the current time so you wanna use time()
@Rakesh Wait what do you mean? What part of the current time do you wanna add? the hour:sec:minute? try: strtotime(date('H:i:s'))+strtotime($mysql_time)
@Rakesh Your looking for interval not to add the two together. Look at feela's answer below.
|
4

Following answer does not return correct value. It is summing integers but not returned correct time.

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

I created a function to get calculated time as follows.

public function addTwoTimes($time1 = "00:00:00", $time2 = "00:00:00"){
        $time2_arr = [];
        $time1 = $time1;
        $time2_arr = explode(":", $time2);
        //Hour
        if(isset($time2_arr[0]) && $time2_arr[0] != ""){
            $time1 = $time1." +".$time2_arr[0]." hours";
            $time1 = date("H:i:s", strtotime($time1));
        }
        //Minutes
        if(isset($time2_arr[1]) && $time2_arr[1] != ""){
            $time1 = $time1." +".$time2_arr[1]." minutes";
            $time1 = date("H:i:s", strtotime($time1));
        }
        //Seconds
        if(isset($time2_arr[2]) && $time2_arr[2] != ""){
            $time1 = $time1." +".$time2_arr[2]." seconds";
            $time1 = date("H:i:s", strtotime($time1));
        }

        return date("H:i:s", strtotime($time1));
    }

2 Comments

Yeah your scenario was a bit more complex and required more use of strototime
Yes. That's true. We can use this kind of solution until find a better approach to solve that
2

You could use the PHP 5.3 DateInterval:

$timeInterval = DateInterval::createFromDateString( '15 hours + 20 minutes' );
$timeInterval2 = DateInterval::createFromDateString( '30 minutes' );

foreach( str_split( 'ymdhis' ) as $prop )
{
    $timeInterval->$prop += $timeInterval2->$prop;
}
var_dump( $timeInterval->format( '%H:%i:%s' ) );

(How to add to DateInterval objects was explained here: How we can add two date intervals in PHP)

Comments

0

As far as I can tell, Sammaye's answer did't work out for me.

I needed to start the time I wanted to add with the start of the UNIX timestamp. This way, strtotime returns the seconds that need to be added to the first time.

$time1 = "15:20:00";
$time2 = "1970-01-01 00:30:00";
$time = strtotime($time1) + (strtotime($time2) + 3600);
echo $time . "<br />";
echo date("H:i:s", $time);

Comments

0

Be sure to consult the mystic docs https://www.php.net/strtotime for additional things you can input into your functions :)

$today = time();
$tommorrow = strtotime("+1 days", $today);
$day_after_tomorrow = strtotime("+1 days", $tomorrow);

Comments

-1

Code:

$time1 = '15:20:00';
$time2 = '00:30:00';
$time = strtotime($time1)+strtotime($time2);
$sumtime = date("H:i:s",$time);

2 Comments

Hi sumesh how to add current time + $time2 ..?
This one should return 15:50:00 but returns 09:21:44

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.