PHP 秒数转换成时分秒的格式

**代码中引用的php函数
sprintff—返回一个格式化的字符串
intval — 获取变量的整数值

如果可以的话,看到这篇博文的朋友 可以加我的扣扣大家一起交流
扣扣:354-032-267。家相互学习,

"传入要转换的秒数");

function secondsToHour($seconds){
   if(intval($seconds) < 60)
        $tt ="00:00:".sprintf("%02d",intval($seconds%60));
   if(intval($seconds) >=60){
        $h =sprintf("%02d",intval($seconds/60));
        $s =sprintf("%02d",intval($seconds%60));
        if($s == 60){
            $s = sprintf("%02d",0);
            ++$h;
        }
        $t = "00";
       if($h == 60){
            $h = sprintf("%02d",0);
            ++$t;
       }
       if($t){
            $t  = sprintf("%02d",$t);
       }
       $tt= $t.":".$h.":".$s;
    }
    if(intval($seconds)>=60*60){
       $t= sprintf("%02d",intval($seconds/3600));
       $h =sprintf("%02d",intval($seconds/60)-$t*60);
       $s =sprintf("%02d",intval($seconds%60));
      if($s == 60){
         $s = sprintf("%02d",0);
         ++$h;
       }
       if($h == 60){
            $h = sprintf("%02d",0);
            ++$t;
       }
       if($t){
            $t  = sprintf("%02d",$t);
       }
       $tt= $t.":".$h.":".$s;
    }
    echo  $seconds>0?$tt:'00:00:00';
}

?>

你可能感兴趣的:(代码)