I want only hour minutes and seconds from seconds

I have seconds and I wants to convert it to hh:mm:ss but the case is if seconds exceeds the day then count that part in hours
i.e. 96528 should be 26:48:48 instead of 2 days 02:48:48

Tagged:

Answers

  • sachin
    edited September 2021
    if (!function_exists('secondsToTime')) {
        function secondsToTime($seconds_time = 0){
            if(is_numeric($seconds_time) && $seconds_time > 0){
                $seconds_time = (int)$seconds_time;
    
                $remainder_m = $seconds_time % 3600;
                $hours = ($seconds_time - $remainder_m) / 3600;
    
                $remainder_s = $remainder_m % 60;
                $minutes = ($remainder_m - $remainder_s) / 60;
    
                $seconds = $remainder_s;
    
                $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";
            }else{
                return "00:00:00";
            }
    
        }
    }
    

    echo secondsToTime(96528); will give 26:48:48

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!