PHP时间戳转换为时分秒

醉菜鸟 2022-01-12 08:00:15

function format_time($time, $sign = 0)
{
    $h = $m = $s = 0;
    $str = "";
    $s = floor($time % 60);
    $m = floor($time / 60) % 60;
    $h = floor($time / 60 / 60);
    if( $sign == 0 )
    {
        if( 0 < $h )
        {
            return $str = $h . "时" . $m . "分" . $s . "秒";
        }

        if( 0 < $m )
        {
            return $str = $m . "分" . $s . "秒";
        }

        return $str = $s . "秒";
    }

    if( $sign == 1 )
    {
        if( $m < 10 )
        {
            $m = "0" . $m;
        }

        if( $s < 10 )
        {
            $s = "0" . $s;
        }

        if( 0 < $h )
        {
            return $str = $h . ":" . $m . ":" . $s;
        }

        if( 0 < $m )
        {
            return $str = $m . ":" . $s;
        }

        return $str = "00:" . $s;
    }

    return NULL;
}
意见反馈