18

i have two time values as give below

$time  = 06:58:00;
$time2 = 00:40:00;

I am doing this for calculating the appointments and available time for a particular user so i tried in this way

$max_date=abs(strtotime($time) + strtotime($time2));

but it is returning $max_date =2673452280 any suggestions pls

2
  • 1
    PHP does not have date/time literals, last i checked. Those time values should have quotes around them. Commented May 11, 2012 at 18:57
  • strtotime() returns the unix timestamp for the given string value, eg. strtotime("00:40:00"); would return the number of seconds from January 1st 1970 until "today (server-side time), 00:40 AM". Commented May 11, 2012 at 19:00

8 Answers 8

43

this code sample would take hour in $time and add the hour in $time2 to it

for example: time=06:58:00, time2=00:40:00, result = 07:38:00

$time = "06:58:00";
$time2 = "00:40:00";

$secs = strtotime($time2)-strtotime("00:00:00");
$result = date("H:i:s",strtotime($time)+$secs);
Sign up to request clarification or add additional context in comments.

4 Comments

Warning: strtotime() [function.strtotime]: It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EDT/-4.0/DST' instead on line 3
You can set a time zone just before your time processing function or at the top of your script eg.: date_default_timezone_set('UTC');
This doesn't work if the total is more than 24 hours.
Any clues how to use this example if the time is more than 24 hours?
11

Use this function...

 function sum_the_time($time1, $time2) {
      $times = array($time1, $time2);
      $seconds = 0;
      foreach ($times as $time)
      {
        list($hour,$minute,$second) = explode(':', $time);
        $seconds += $hour*3600;
        $seconds += $minute*60;
        $seconds += $second;
      }
      $hours = floor($seconds/3600);
      $seconds -= $hours*3600;
      $minutes  = floor($seconds/60);
      $seconds -= $minutes*60;
      if($seconds < 9)
      {
      $seconds = "0".$seconds;
      }
      if($minutes < 9)
      {
      $minutes = "0".$minutes;
      }
        if($hours < 9)
      {
      $hours = "0".$hours;
      }
      return "{$hours}:{$minutes}:{$seconds}";
    }

Comments

1

strtotime function takes full-date as an argument and valid format are as following: http://www.php.net/manual/en/datetime.formats.php

You can see that in online PHP manual for the function at http://php.net/manual/en/function.strtotime.php

Comments

1

If you're build those time strings from a database before, you'd probably want to rebuild them to something like this:

$time = "00:06:58";
$time2 = "40 minutes";

$timestamp = strtotime($time." +".$time2);
$endTime = date("d.m.Y H:i:s", $timestamp);

Comments

1

Easiest way to add two times using php is :

1) Convert time from H:i:s (e.g. 08:15:40) format to seconds.
2) do the same for second time value ref:step 1
3) add converted values and store it php variable
4) Now convert total (which is in seconds) to H:i:s
and it works for me.

PHP Script:

$str_time ="08:04:40";

$str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time);

sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);

$hrs_old_seconds = $hours * 3600 + $minutes * 60 + $seconds;

$str_time ="02:10:22";

$str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time);

sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);

$hrs_toadd_seconds = $hours * 3600 + $minutes * 60 + $seconds;

$hrs_old_int1 = $hrs_old_seconds + $hrs_toadd_seconds;

echo $Total=gmdate("H:i:s", $hrs_old_int1);

Result= :10:15:02

Comments

1

Anudeep's solution was great for my use case, but I needed to be able to add negative times as well. Here's a slightly edited version of his code to take and return negative time strings ("-01:01:01" for example):

public static function sum_the_times($time1, $time2)
{
    $times = array($time1, $time2);
    $seconds = 0;
    $negative = false;
    foreach ($times as $time) {
        list($hour,$minute,$second) = explode(':', $time);
        if(substr($hour,0,1) == '-'){
            $seconds -= substr($hour,1)*3600;
            $seconds -= $minute*60;
            $seconds -= $second;
        } else {
            $seconds += $hour*3600;
            $seconds += $minute*60;
            $seconds += $second;
        }
    }
    if (substr($seconds, 0, 1) == '-') {
        $negative = true;
        $seconds = ($seconds * -1);
    }
    $hours = floor($seconds/3600);
    $seconds -= $hours*3600;
    $minutes  = floor($seconds/60);
    $seconds -= $minutes*60;
    if ($seconds < 9) {
        $seconds = "0".$seconds;
    }
    if ($minutes < 9) {
        $minutes = "0".$minutes;
    }
    if ($hours < 9) {
        $hours = "0".$hours;
    }
    return ($negative ? "-" : "")."{$hours}:{$minutes}:{$seconds}";
}

Comments

0

You can try this

$time = "04:00:00";
$time2 = "03:30:00";

$result = date("H:i:s",strtotime($time)+strtotime($time2));
echo $result;

It gives output 07:30:00 but it does not work sometime in different version of operating system. If you want to get sum of time then you can use this code

<?php
   function CalculateTime($time1, $time2) {
      $time1 = date('H:i:s',strtotime($time1));
      $time2 = date('H:i:s',strtotime($time2));
      $times = array($time1, $time2);
      $seconds = 0;
      foreach ($times as $time)
      {
        list($hour,$minute,$second) = explode(':', $time);
        $seconds += $hour*3600;
        $seconds += $minute*60;
        $seconds += $second;
      }
      $hours = floor($seconds/3600);
      $seconds -= $hours*3600;
      $minutes  = floor($seconds/60);
      $seconds -= $minutes*60;
      if($seconds < 9)
      {
      $seconds = "0".$seconds;
      }
      if($minutes < 9)
      {
      $minutes = "0".$minutes;
      }
        if($hours < 9)
      {
      $hours = "0".$hours;
      }
      return "{$hours}:{$minutes}:{$seconds}";
    }

    $time1= '23:32:05';
    $time2 = '01:29';
    echo CalculateTime($time1,$time2);

?>

In the second code, you can send time in hour:minutes or hours:minutes:seconds. This code accept both format because it convert time automatically

Comments

0

Here's a version that will cater for over 24 hours and doesn't use strtotime:

$time0 = "24:01:02";
$time1 = "01:02:03";
$matches0 = explode(':',$time0); // split up the string
$matches1 = explode(':',$time1);
$sec0 = $matches0[0]*60*60+$matches0[1]*60+$matches0[2];
$sec1 = $sec0+ $matches1[0]*3600+$matches1[1]*60+$matches1[2]; // get total seconds
$h = intval(($sec1)/3600);
$m = intval(($sec1-$h*3600)/60);
$s = $sec1-$h*3600-$m*60;
echo $str = str_pad($h, 2, '0', STR_PAD_LEFT).':'.str_pad($m, 2, '0', STR_PAD_LEFT).':'.str_pad($s, 2, '0', STR_PAD_LEFT);

Comments

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.