時:分:秒轉秒 , 秒傳時:分:秒

/**
 * 把HH:MM:SS格式的時間字符串轉換成秒數,可以使用date_parse函數解析具體的時間信息。
 * @param $time
 * @return float|int|mixed 反回秒數
 * 2020/5/29
 * 22:10
 */
function changeTimeFormat($time='')
{
    if (empty($time)){
        $time=date('H:i:s',time());
    }
    $count=count(explode(':',$time));
    if ($count==2){
        $time='00:'.$time;
    }elseif($count==1){
        $time='00:00:'.$time;
    }
    $parsed = date_parse($time);
    $seconds = $parsed['hour'] * 3600+$parsed['minute'] * 60+$parsed['second'];
    return $seconds;
}
/**
 * $seconds = 3600*34 + 122;
 * @param $seconds
 * @return string  返回 時:分:秒 | 分:秒  | 秒
 * 2020/5/29
 * 21:51
 */

function changeTimeTypes($seconds){
    if ($seconds >= 3600){
        $hours = intval($seconds/3600);
        $minutes = $seconds % 3600;
        $time = $hours.":".gmstrftime('%M:%S', $minutes);
    }else{
        if ($seconds>=60){
            //        $time = gmstrftime('%H:%M:%S', $seconds);
            $time = gmstrftime('%M:%S', $seconds);
        }else{
            $time = gmstrftime('%S', $seconds);
        }
    }
    return $time;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章