zoukankan      html  css  js  c++  java
  • 格式化时间的函数

    //格式化时间
    function time_format($time, $start='d', $keep='s', $unit=true){
        
        $day = $hours = $minutes = $seconds = '';
        if($start == 'd'){
            //$time / 3600 得到小时数,再除以24小时,余数则为不足一天的小时数
            $day = floor($time / 3600 / 24);
            $hours = floor($time / 3600) % 24;
            $minutes = floor($time / 60) % 60;
            $seconds = $time % 60;
        }elseif($start == 'h'){
            $hours = floor($time / 3600);
            $minutes = floor($time / 60) % 60;
            $seconds = $time % 60;
        }elseif($start == 'i'){
            $minutes = floor($time / 60);
            $seconds = $time % 60;
        }
        
        //如果要带单位
        if($unit){
            $day = ($day)? $day.'天' : '';
            $hours = ($hours)? $hours.'小时' : '';
            $minutes = ($minutes)? $minutes.'分钟' : '';
            $seconds = ($seconds)? $seconds.'秒' : '';
        }
        
        $new['d'] = $day;
        $new['h'] = $hours;
        $new['i'] = $minutes;
        $new['s'] = $seconds;
        
        $time = '';
        foreach($new as $key => $val){
            if($key == $start) $go = true;
            if($go){
                if($unit){
                    $time .= $val;
                }else{
                    $time[$key]= $val;
                }
            }
            if($key == $keep) break;
        }
        return $time;
    }
  • 相关阅读:
    POJ3613 k边最短路
    洛谷4014最大/小费用最大流
    POJ1734无向图求最小环
    洛谷4013数字梯形
    洛谷4147玉蟾宫
    洛谷4145上帝造题的七分钟2
    洛谷4092 树
    Nginx动静分离-tomcat
    nginx之Geoip读取地域信息模块
    Nginx与Lua开发
  • 原文地址:https://www.cnblogs.com/tudou1223/p/4935205.html
Copyright © 2011-2022 走看看