Convert seconds to Hour:Minute:Second

前端 未结 27 2711
说谎
说谎 2020-11-22 07:56

I need to convert seconds to \"Hour:Minute:Second\".

For example: \"685\" converted to \"00:11:25\"

How can I achieve this?

27条回答
  •  佛祖请我去吃肉
    2020-11-22 08:21

    Well I needed something that would reduce seconds into hours minutes and seconds, but would exceed 24 hours, and not reduce further down into days.

    Here is a simple function that works. You can probably improve it... But here it is:

    function formatSeconds($seconds)
    {
        $hours = 0;$minutes = 0;
        while($seconds >= 60){$seconds -= 60;$minutes++;}
        while($minutes >= 60){$minutes -=60;$hours++;}
        $hours = str_pad($hours, 2, '0', STR_PAD_LEFT);
        $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT);
        $seconds = str_pad($seconds, 2, '0', STR_PAD_LEFT);
        return $hours.":".$minutes.":".$seconds;
    }
    

提交回复
热议问题