Home  >  Article  >  php教程  >  计算两个时间戳之间的时间长度

计算两个时间戳之间的时间长度

WBOY
WBOYOriginal
2016-06-07 11:44:151290browse

此方法主要用于计算两个时间长度的有效数,比如,*年*月*日*时*分*秒,月份采用30天为一月,年采用365天为一年,计算有些许出入。
    /**<br>      * 返回两个时间的相距时间,*年*月*日*时*分*秒<br>      * @param int $one_time 时间一<br>      * @param int $two_time 时间二<br>      * @param int $return_type 默认值为0,0/不为0则拼接返回,1/*秒,2/*分*秒,3/*时*分*秒/,4/*日*时*分*秒,5/*月*日*时*分*秒,6/*年*月*日*时*分*秒<br>      * @param array $format_array 格式化字符,例,array('年', '月', '日', '时', '分', '秒')<br>      * @return String or false<br>      */<br>     public function getRemainderTime($one_time, $two_time, $return_type=0, $format_array=array('年', '月', '日', '时', '分', '秒')){<br>         if($return_type6){<br>             return false;<br>         }<br>         if(!(is_int($one_time) && is_int($two_time))){<br>             return false;<br>         }<br>         $remainder_seconds = abs($one_time-$two_time);<br>         //年<br>         $years = 0;<br>         if(($return_type==0 || $return_type==6) && $remainder_seconds-31536000>0){<br>             $years = floor($remainder_seconds/(31536000));<br>         }<br>         //月<br>         $monthes = 0;<br>         if(($return_type==0 || $return_type>=5) && $remainder_seconds-$years*31536000-2592000>0){<br>             $monthes = floor(($remainder_seconds-$years*31536000)/(2592000));<br>         }<br>         //日<br>         $days = 0;<br>         if(($return_type==0 || $return_type>=4) && $remainder_seconds-$years*31536000-$monthes*2592000-86400>0){<br>             $days = floor(($remainder_seconds-$years*31536000-$monthes*2592000)/(86400));<br>         }<br>         //时<br>         $hours = 0;<br>         if(($return_type==0 || $return_type>=3) && $remainder_seconds-$years*31536000-$monthes*2592000-$days*86400-3600>0){<br>             $hours = floor(($remainder_seconds-$years*31536000-$monthes*2592000-$days*86400)/3600);<br>         }<br>         //分<br>         $minutes = 0;<br>         if(($return_type==0 || $return_type>=2) && $remainder_seconds-$years*31536000-$monthes*2592000-$days*86400-$hours*3600-60>0){<br>             $minutes = floor(($remainder_seconds-$years*31536000-$monthes*2592000-$days*86400-$hours*3600)/60);<br>         }<br>         //秒<br>         $seconds = $remainder_seconds-$years*31536000-$monthes*2592000-$days*86400-$hours*3600-$minutes*60;<br>         $return = false;<br>         switch ($return_type){<br>             case 0:<br>                 if($years>0){<br>                     $return = $years.$format_array[0].$monthes.$format_array[1].$days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];<br>                 }else if($monthes>0){<br>                     $return = $monthes.$format_array[1].$days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];<br>                 }else if($days>0){<br>                     $return = $days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];<br>                 }else if($hours>0){<br>                     $return = $hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];<br>                 }else if($minutes>0){<br>                     $return = $minutes.$format_array[4].$seconds.$format_array[5];<br>                 }else{<br>                     $return = $seconds.$format_array[5];<br>                 }<br>                 break;            <br>             case 1:<br>                 $return = $seconds.$format_array[5];<br>                 break;<br>             case 2:<br>                 $return = $minutes.$format_array[4].$seconds.$format_array[5];<br>                 break;<br>             case 3:<br>                 $return = $hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];<br>                 break;<br>             case 4:<br>                 $return = $days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];<br>                 break;<br>             case 5:<br>                 $return = $monthes.$format_array[1].$days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];<br>                 break;<br>             case 6:<br>                 $return = $years.$format_array[0].$monthes.$format_array[1].$days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];<br>                 break;<br>             default:<br>                 $return = false;                <br>         }<br>         return $return;<br>     }

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:求TPM示例Next article:分享:7套bootstrap模版