- /**
- * 返回两个时间的相距时间,*年*月*日*时*分*秒
- * @param int $one_time 时间一
- * @param int $two_time 时间二
- * @param int $return_type 默认值为0,0/不为0则拼接返回,1/*秒,2/*分*秒,3/*时*分*秒/,4/*日*时*分*秒,5/*月*日*时*分*秒,6/*年*月*日*时*分*秒
- * @param array $format_array 格式化字符,例,array('年', '月', '日', '时', '分', '秒')
- * @return String or false
- */
- public function getRemainderTime($one_time, $two_time, $return_type=0, $format_array=array('年', '月', '日', '时', '分', '秒')){
- if($return_type6){
- return false;
- }
- if(!(is_int($one_time) && is_int($two_time))){
- return false;
- }
- $remainder_seconds = abs($one_time-$two_time);
- //年
- $years = 0;
- if(($return_type==0 || $return_type==6) && $remainder_seconds-31536000>0){
- $years = floor($remainder_seconds/(31536000));
- }
- //月
- $monthes = 0;
- if(($return_type==0 || $return_type>=5) && $remainder_seconds-$years*31536000-2592000>0){
- $monthes = floor(($remainder_seconds-$years*31536000)/(2592000));
- }
- //日
- $days = 0;
- if(($return_type==0 || $return_type>=4) && $remainder_seconds-$years*31536000-$monthes*2592000-86400>0){
- $days = floor(($remainder_seconds-$years*31536000-$monthes*2592000)/(86400));
- }
- //时
- $hours = 0;
- if(($return_type==0 || $return_type>=3) && $remainder_seconds-$years*31536000-$monthes*2592000-$days*86400-3600>0){
- $hours = floor(($remainder_seconds-$years*31536000-$monthes*2592000-$days*86400)/3600);
- }
- //分
- $minutes = 0;
- if(($return_type==0 || $return_type>=2) && $remainder_seconds-$years*31536000-$monthes*2592000-$days*86400-$hours*3600-60>0){
- $minutes = floor(($remainder_seconds-$years*31536000-$monthes*2592000-$days*86400-$hours*3600)/60);
- }
- //秒
- $seconds = $remainder_seconds-$years*31536000-$monthes*2592000-$days*86400-$hours*3600-$minutes*60;
- $return = false;
- switch ($return_type){
- case 0:
- if($years>0){
- $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];
- }else if($monthes>0){
- $return = $monthes.$format_array[1].$days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];
- }else if($days>0){
- $return = $days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];
- }else if($hours>0){
- $return = $hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];
- }else if($minutes>0){
- $return = $minutes.$format_array[4].$seconds.$format_array[5];
- }else{
- $return = $seconds.$format_array[5];
- }
- break;
- case 1:
- $return = $seconds.$format_array[5];
- break;
- case 2:
- $return = $minutes.$format_array[4].$seconds.$format_array[5];
- break;
- case 3:
- $return = $hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];
- break;
- case 4:
- $return = $days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];
- break;
- case 5:
- $return = $monthes.$format_array[1].$days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];
- break;
- case 6:
- $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];
- break;
- default:
- $return = false;
- }
- return $return;
- }
复制代码
|
PHP
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31