Help Needed for addition of two variables Urgent...

Discussion in 'PHP' started by raju79, May 26, 2007.

  1. #1
    I need to sum two variable each of them have time in time format how can i add them to have result in the same time format.

    $c1 = 29:00:00
    $c2 = 05:10:00


    $c = $c1 + $c2;

    when i echo $c it gives result as 34
    But i need result as 34:10:00

    Please help me out.


    Thanks
     
    raju79, May 26, 2007 IP
  2. projectshifter

    projectshifter Peon

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Eh, a couple ways, a quick and simple one would be:
    $ca = explode(':', $c1);
    $cb = explode(':', $c2);
    $c = array($ca[0] + $cb[0], $ca[1] + $cb[1], $ca[2] + $cb[2]);
    if ($c[2] > 60){
    $c[2] -= 60;
    $c[1] += 1;
    }
    if ($c[1] > 60){
    $c[1] -= 60;
    $c[0] += 1;
    }

    $c = str_pad($c[0], 2, '0', STR_PAD_LEFT).':'str_pad($c[1], 2, '0', STR_PAD_LEFT).':'.str_pad($c[2], 2, '0', STR_PAD_LEFT);

    Go ahead and give that a try, it should work.
     
    projectshifter, May 26, 2007 IP