add two or more time strings in php

前端 未结 3 1210
后悔当初
后悔当初 2020-12-22 02:31

I have an array with times (string) e.g \"2:23\", \"3:2:22\" etc.

$times = array(\"2:33\", \"4:2:22\", \"3:22\") //loner

I want to find the

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-22 02:58

    So you want to add a duration to a time, meaning add a number of hours, minutes and seconds to a time?

    Perhaps you might use Dav's strtotime to get the seconds from midnight for the time, then add the duration using an sscanf, thus:

    $midnight = strtotime("0:00");
    
    // ssm = seconds since midnight
    $ssm1 = strtotime("2:33") - $midnight;
    
    // sscanf the time string... you might need to include the possibility of seconds
    list($hour, $minute) = sscanf("3:33", "%d:%d");
    
    // a
    $answer_ssm = $ssm + $hour * 3600 + $minute * 60;
    
    echo strftime("%H:%M:%S", $answer_ssm);

    Can't test this from where I am so pls excuse any syntax errors... just trying to give an example. You'll also need to iterate across your array of course.

提交回复
热议问题