3
    $test=strtotime("06:30:00")+strtotime("03:00:00");

    echo gmdate("H:i:s", $test);

This gives the wrong answer ( 23:01:44 ).

    $test=strtotime("06:30:00")-strtotime("03:00:00");

    echo gmdate("H:i:s", $test);

while subtracting same code gives me the right answer( 03:30:00 ).

5
  • You cannot add/subtract points in time. Add/subtract TimeSpans instead. Or calc back to seconds since epoch en add/subtract those. Commented Oct 11, 2013 at 10:16
  • can you give me an example with code in php? Commented Oct 11, 2013 at 10:20
  • you will have to tell what you want. The code you provided is nonsense. strtotime will give you a point in (absolute) time. You cannot add another absolute timestamp to that. If you are working with time durations, strtotime is the wrong tool. Commented Oct 11, 2013 at 10:22
  • I am sorry but i am a beginner, Actually I have a loop in my code which give time durations (in hour and minutes) and i want to calculate the total of that. Commented Oct 11, 2013 at 10:26
  • I think my answer may help you Here is the Solution : stackoverflow.com/a/37893459/3164430 Commented Apr 7, 2017 at 8:06

4 Answers 4

3

You should do this:

<?
echo date("H:i:s", strtotime("06:30:00 + 3 hour"));
echo date("H:i:s", strtotime("06:30:00 - 3 hour"));
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank You... This is what i needed, and i got it from your answer. :) echo date("H:i:s", strtotime("06:30:00 - 3 hour 30 minutes"));
Hope you got my back all the time.. :)
1

Your logic is wrong. If you'll act like in 1-st sample, you'll sum two timestamps as they are (i.e. starting from 0) - so you'll count previous time twice. If you are subtracting them, however, you're getting 'expected' result because each operand's previous period eliminates another.

But that's not how it's supposed to work. You should use DateTime API to work with dates and periods in PHP.

1 Comment

I am sorry but i am a beginner, Actually I have a loop in my code which give time durations (in hour and minutes) and i want to calculate the total of that.
0

I came across a similar problem, and wrote a small function to convert times (formatted in a variety of ways) to seconds. It's obviously then trivial to add the seconds together.

function getSeconds($s) {
    $s = str_replace('h',':',$s);
    $s = str_replace(',','.',$s);

    $a = explode(':',$s);
    $n = count($a);

    if ($n==1)
        return $a[0]; //just seconds

    if ($n==2)
        return $a[1] + 60 * $a[0]; //minutes and seconds

    if ($n==3)
        return $a[2] + 60 * $a[1] + 3600 * $a[0]; //hours minutes and seconds
}

Works for the following formats:

 12
 12.34
 12:34 (will assume mm:ss in this format)
 12:34.56
 12:34,56
 12:34:56.78
 12h34:56,78

Comments

0
 $date1 = strtotime('2012-05-01 12:00:00');
 $date2 = time();
 $subTime = $date1 - $date2;
 $y = ($subTime/(60*60*24*365));
 $d = ($subTime/(60*60*24))%365;
 $h = ($subTime/(60*60))%24;
 $m = ($subTime/60)%60;

 echo "Difference between ".date('Y-m-d H:i:s',$date1)." and ".date('Y-m-d H:i:s',$date2)." is:\n";
 echo $y." years\n";
 echo $d." days\n";
 echo $h." hours\n";
 echo $m." minutes\n";

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.