毫秒数转换为对应年、小时、分、秒、毫秒

谭佳成
2017-11-07
(305)

    /**
     * 毫秒转换为时间
     * @param  string  $seconds [毫秒数] 315532
     * @return [string]      [xx年xx小时xx分xx秒xxxx毫秒]
     */
    function secondtodate($seconds = '')
    {
        if(strlen(trim($seconds)) < 1){
            return false;
        }
        $str = '';
        $type = substr($seconds,0,1);
        $str .= ($type == '-')?'提前':'延后';
        $seconds = abs($seconds);
        $value = array(
          "years" => 0,
          "days" => 0,
          "hours" => 0,
          "minutes" => 0,
          "seconds" => 0,
          "millisecond" => 0
        );
        if($seconds >= 31556926000){
          $value["years"] = floor($seconds/31556926000);
          $str .= $value["years"] .'年';
          $seconds = ($seconds%31556926000);
        }
        if($seconds >= 86400000){
          $value["days"] = floor($seconds/86400000);
          $str .= $value["days"] .'天';
          $seconds = ($seconds%86400000);
        }
        if($seconds >= 3600000){
          $value["hours"] = floor($seconds/3600000);
          $str .= $value["hours"] .'小时';
          $seconds = ($seconds%3600000);
        }
        if($seconds >= 60000){
          $value["minutes"] = floor($seconds/60000);
          $str .= $value["minutes"] .'分';
          $seconds = ($seconds%60000);
        }
        if($seconds >= 1000){
          $value["seconds"] = floor($seconds/1000);
          $str .= $value["seconds"] .'秒';
          $seconds = ($seconds%1000);
        }
        $value["millisecond"] = floor($seconds);
        $str .= $value["millisecond"] .'毫秒';
        return $str;
    }


如无说明,本站文章均为原创,转载或引用注明来源:https://93jc.cn/article/137.html