php 时分秒转为秒,秒转化为天时分秒

一、分秒转为秒

/*
 * 获取时分秒,转化成秒
 * 8小时5分5秒
 */
function turnSecond($str){
    $hour = 0;
    $minute = 0;
    $second = 0;
    if(strpos($str,'时') !== false){ 
        $strArr1 = explode('时',$str);
        $hour = $strArr1[0];
        $strArr2 = explode('分',$strArr1[1]);
        $minute = $strArr2[0];
        $strArr3 = explode('秒',$strArr2[1]);
        $second = $strArr3[0];           
    }else{
        if(strpos($str,'分') !== false){
            $strArr1 = explode('分',$str);
            $minute = $strArr1[0];
            $strArr2 = explode('秒',$strArr1[1]);
            $second = $strArr2[0];
        }else{
            if(strpos($str,'秒') !== false){
                $strArr = explode('秒',$str);
                $second = $strArr[0];
            }
        }
    }
    
    $num = $hour * 3600 + $minute * 60 + $second;
    return $num;
}

二、秒转化为天时分秒

/*
  转化时间为 天时分秒
 */
function timesecond($seconds){
    $days_num = '0:';
    $seconds = (int)$seconds;
    if($seconds > 3600 ){
        if($seconds > 24*3600){
            $days  = (int)($seconds/86400);
            $days_num = $days.":";
            $seconds = $seconds%86400;//取余
        }
        $hours = intval($seconds/3600);
        $minutes = $seconds%3600;//取余下秒数
        $time = $days_num.$hours.":".gmstrftime('%M:%S',$minutes);
    }else{
        $time = $days_num.gmstrftime('%H:%M:%S', $seconds);
    }

    //替换字符 ,因为在函数中用分钟和秒我的无法正常输出,于是改了写法
    $arr = explode(':',$time);

    return $arr[0].'天'.$arr[1].'小时'.$arr[2].'分钟'.$arr[3].'秒';
}

 

你可能感兴趣的:(ThinkPHP6,PHP,ThinkPHP)