简体   繁体   中英

Convert timestamp to time and keep seconds, minutes and hour

I have a function to convert timestamp to real hour, minute and second, I would to keep the format like: hh:mm:ss even if it is only some minutes and seconds, I should get 00:10:32 instead of 10:32:

// format can be either : or blank
function toTime($timestamp, $format)
{
    $hours = floor($timestamp / 3600);
    $minutes = floor($timestamp % 3600 / 60);
    $seconds = $timestamp % 60;

            if($format == ':') {
                $hourDuration = sprintf('%02d:', $hours);
                //echo 'hour: '.$hourDuration.'<br />';
                $minDuration =  sprintf('%02d:', $minutes);
                //echo 'min: '.$minDuration.'<br />';
                $secDuration =  sprintf('%02d', $seconds);
                //echo 'sec: '.$secDuration.'<br />';
                $HourMinSec = $hourDuration.$minDuration.$secDuration;
            } else {
                $hourDuration = sprintf('%02d h', $hours);
                //echo 'hour: '.$hourDuration.'<br />';
                $minDuration =  sprintf('%02d m', $minutes);
                //echo 'min: '.$minDuration.'<br />';
                $secDuration =  sprintf('%02d s', $seconds);
                //echo 'sec: '.$secDuration.'<br />';
                $HourMinSec = '';
            }

            if($hourDuration > 0){
                $hourDuration = $hourDuration;
            } else {
                $hourDuration = '';
            }

            if($minDuration > 0){
                $minDuration = $minDuration;
            } else {
                $minDuration = '';
            }

            if($secDuration > 0){
                $secDuration = $secDuration;
            } else {
                $secDuration = '';
            }

    //$HourMinSec = $hourDuration.'&nbsp;'.$minDuration.'&nbsp;'.$secDuration;
    $HourMinSec = $hourDuration.$minDuration.$secDuration;

    return $HourMinSec;
}

This function is used to get a video timestamp like: 1508.7397460938, and I would like to sort out, how many hours, minutes and seconds are in that video

Thanks for your assistance

I would have done it that way

function toTime($timestamp, $format)
    {
        $hours = floor($timestamp / 3600);
        $minutes = floor($timestamp % 3600 / 60);
        $seconds = $timestamp % 60;

                if($format == ':') {
                    $hourDuration = sprintf('%02d:', $hours);
                    //echo 'hour: '.$hourDuration.'<br />';
                    $minDuration =  sprintf('%02d:', $minutes);
                    //echo 'min: '.$minDuration.'<br />';
                    $secDuration =  sprintf('%02d', $seconds);
                    //echo 'sec: '.$secDuration.'<br />';
                    $HourMinSec = $hourDuration.$minDuration.$secDuration;
                } else {
                    $hourDuration = sprintf('%02d h', $hours);
                    //echo 'hour: '.$hourDuration.'<br />';
                    $minDuration =  sprintf('%02d m', $minutes);
                    //echo 'min: '.$minDuration.'<br />';
                    $secDuration =  sprintf('%02d s', $seconds);
                    //echo 'sec: '.$secDuration.'<br />';
                    $HourMinSec = '';
                }

                if($hourDuration > 0){
                    $hourDuration = $hourDuration;
                } else {
                    $hourDuration = ($format == ':' ? '00:' : '00 h');
                }

                if($minDuration > 0){
                    $minDuration = $minDuration;
                } else {
                    $minDuration = ($format == ':' ? '00:' : '00 m');
                }

                if($secDuration > 0){
                    $secDuration = $secDuration;
                } else {
                    $secDuration = '00';
                }

        //$HourMinSec = $hourDuration.'&nbsp;'.$minDuration.'&nbsp;'.$secDuration;
        $HourMinSec = $hourDuration.$minDuration.$secDuration;

        return $HourMinSec;
    }

In that case it would bring you the following result for adding

echo toTime(1508.7397460938);
Output --> 00 h25 m08 s
echo toTime(1508.7397460938,':');
Output --> 00:25:08

The other variant is to use the php ready function gmdate by typing:

echo gmdate("H:i:s", 1508.7397460938);

// Use that only if the amount of hours is less than 24 , that function would now show more than 24 hours and if so it would show inaccurate data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM