Jump to content

question about addimg hours not in date format


hammerklavier

Recommended Posts

 

 

This function

 

       

function SumaHoras( $time1, $time2 )
        {
            list($hour1, $min1, $sec1) = parteHora($time1);
            list($hour2, $min2, $sec2) = parteHora($time2);

            return date('H:i:s', mktime( $hour1 + $hour2, $min1 + $min2, $sec1 + $sec2));
}  

 

echoes returns 05:33

 

when implemented

 

$totaltotal = SumaHoras($currenttime,$secondtime);

 

values $time1 and $time2 are

 

14:32

15:01

 

I want it to return 29:33.... how do I do that ?

 

Thanks in advance

return ($hour1 + $hour2) . ":" . ($min1 + $min2) + ceil(($sec1 + $sec2) / 60);

 

Should work. Untested however.

 

That won't work. The times 12:33 + 10:45 would come out to 22:78. The script needs to account for times where the seconds and/or minutes would add up to more than 60 to add to the next higher increment

 

Tested:

function SumaHoras($time1, $time2)
{
    list($hour1, $min1, $sec1) = explode(':', $time1);
    list($hour2, $min2, $sec2) = explode(':', $time2);

    $secs  = ($sec1 + $sec2) % 60;
    $mins  = ($min1 + $min2 + floor(($sec1+$sec2-$secs)/60)) % 60;
    $hours = ($hour1 + $hour2 + floor(($min1+$min2+floor(($sec1+$sec2-$secs)/60)-$mins)/60));

    return sprintf("%s:%02s:%02s", $hours, $mins, $secs);
}

return ($hour1 + $hour2) . ":" . ($min1 + $min2) + ceil(($sec1 + $sec2) / 60);

 

Should work. Untested however.

 

That won't work. The times 12:33 + 10:45 would come out to 22:78. The script needs to account for times where the seconds and/or minutes would add up to more than 60 to add to the next higher increment

 

Tested:

function SumaHoras($time1, $time2)
{
    list($hour1, $min1, $sec1) = explode(':', $time1);
    list($hour2, $min2, $sec2) = explode(':', $time2);

    $secs  = ($sec1 + $sec2) % 60;
    $mins  = ($min1 + $min2 + floor(($sec1+$sec2-$secs)/60)) % 60;
    $hours = ($hour1 + $hour2 + floor(($min1+$min2+floor(($sec1+$sec2-$secs)/60)-$mins)/60));

    return sprintf("%s:%02s:%02s", $hours, $mins, $secs);
}

 

Nice, yea my mind isn't working today. Thanks for correcting that =)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.