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;
Lexib0y
  • 519
  • 10
  • 27
Rakesh
  • 273
  • 3
  • 4
  • 10

7 Answers7

7

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);
luissquall
  • 1,740
  • 19
  • 14
  • 2
    This is great and simple but it would be more relevant if you can provide further explanations about subtracting that 0 value time. – rhavendc Sep 19 '17 at 03:43
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.

Sammaye
  • 43,242
  • 7
  • 104
  • 146
  • The result should added current time + a time variable which i am taking from DB say $tim2 – Rakesh Jul 30 '12 at 11:50
  • $time = date("H:i:s")+strtotime($mysql_time); you mean this way..? – Rakesh Jul 30 '12 at 12:01
  • @Rakesh No, time() returns a timestamp of the current time so you wanna use time() – Sammaye Jul 30 '12 at 12:01
  • @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)` – Sammaye Jul 30 '12 at 12:03
  • @Rakesh Your looking for interval not to add the two together. Look at feela's answer below. – Sammaye Jul 30 '12 at 12:32
  • @cha what is your code, you might find that the strtotime is not correctly functioning, but this is summing two integers, i.e. 1+1 as such this code cannot really fail without PHPs core math library failing – Sammaye Jun 02 '16 at 14:37
  • @Sammaye Yes. This is summing two integers. But finally returned wrong answer. I created a simple function. Please see my answer below. – cha Jun 03 '16 at 08:06
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));
    }
cha
  • 730
  • 2
  • 12
  • 28
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)

Community
  • 1
  • 1
feeela
  • 29,399
  • 7
  • 59
  • 71
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);
Patrick G
  • 486
  • 2
  • 12
0

Be sure to consult the mystic docs http://us1.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);
britter
  • 137
  • 6
-1

Code:

$time1 = '15:20:00';
$time2 = '00:30:00';
$time = strtotime($time1)+strtotime($time2);
$sumtime = date("H:i:s",$time);
Sumesh TG
  • 440
  • 1
  • 4
  • 15