PHP计算两个时间之差的函数(年,月,周,日,小时,分钟,秒数)
PHP计算两个时间之差的函数(年,月,周,日,小时,分钟,秒数)
1 <? PHP
2 // 时间格式为YYYYMMDDHHmmss
3 function timeDiff( $aTime , $bTime )
4 {
5 // 分割第一个时间
6 $ayear = substr ( $aTime , 0 , 4 );
7 $amonth = substr ( $aTime , 4 , 2 );
8 $aday = substr ( $aTime , 6 , 2 );
9 $ahour = substr ( $aTime , 8 , 2 );
10 $aminute = substr ( $aTime , 10 , 2 );
11 $asecond = substr ( $aTime , 12 , 2 );
12 // 分割第二个时间
13 $byear = substr ( $bTime , 0 , 4 );
14 $bmonth = substr ( $bTime , 4 , 2 );
15 $bday = substr ( $bTime , 6 , 2 );
16 $bhour = substr ( $bTime , 8 , 2 );
17 $bminute = substr ( $bTime , 10 , 2 );
18 $bsecond = substr ( $bTime , 12 , 2 );
19 // 生成时间戳
20 $a = mktime ( $ahour , $aminute , $asecond , $amonth , $aday , $ayear );
21 $b = mktime ( $bhour , $bminute , $bsecond , $bmonth , $bday , $byear );
22 $timeDiff [ ' second ' ] = $a - $b ;
23 // 采用了四舍五入,可以修改
24 $timeDiff [ ' mintue ' ] = round ( $timeDiff [ ' second ' ] / 60 );
25 $timeDiff [ ' hour ' ] = round ( $timeDiff [ ' mintue ' ] / 60 );
26 $timeDiff [ ' day ' ] = round ( $timeDiff [ ' hour ' ] / 24 );
27 $timeDiff [ ' week ' ] = round ( $timeDiff [ ' day ' ] / 7 );
28 $timeDiff [ ' month ' ] = round ( $timeDiff [ ' day ' ] / 30 ); // 按30天来算
29 $timeDiff [ ' year ' ] = round ( $timeDiff [ ' day ' ] / 365 ); // 按365天来算
30 return $timeDiff ;
31 }
32 ?>
用法:
$a=timeDiff("20060613161000","20040613150000");
print_r($a);
返回一个数组,可以用下标直接输出来年,月,周,日,小时,分钟,秒数之差
2 // 时间格式为YYYYMMDDHHmmss
3 function timeDiff( $aTime , $bTime )
4 {
5 // 分割第一个时间
6 $ayear = substr ( $aTime , 0 , 4 );
7 $amonth = substr ( $aTime , 4 , 2 );
8 $aday = substr ( $aTime , 6 , 2 );
9 $ahour = substr ( $aTime , 8 , 2 );
10 $aminute = substr ( $aTime , 10 , 2 );
11 $asecond = substr ( $aTime , 12 , 2 );
12 // 分割第二个时间
13 $byear = substr ( $bTime , 0 , 4 );
14 $bmonth = substr ( $bTime , 4 , 2 );
15 $bday = substr ( $bTime , 6 , 2 );
16 $bhour = substr ( $bTime , 8 , 2 );
17 $bminute = substr ( $bTime , 10 , 2 );
18 $bsecond = substr ( $bTime , 12 , 2 );
19 // 生成时间戳
20 $a = mktime ( $ahour , $aminute , $asecond , $amonth , $aday , $ayear );
21 $b = mktime ( $bhour , $bminute , $bsecond , $bmonth , $bday , $byear );
22 $timeDiff [ ' second ' ] = $a - $b ;
23 // 采用了四舍五入,可以修改
24 $timeDiff [ ' mintue ' ] = round ( $timeDiff [ ' second ' ] / 60 );
25 $timeDiff [ ' hour ' ] = round ( $timeDiff [ ' mintue ' ] / 60 );
26 $timeDiff [ ' day ' ] = round ( $timeDiff [ ' hour ' ] / 24 );
27 $timeDiff [ ' week ' ] = round ( $timeDiff [ ' day ' ] / 7 );
28 $timeDiff [ ' month ' ] = round ( $timeDiff [ ' day ' ] / 30 ); // 按30天来算
29 $timeDiff [ ' year ' ] = round ( $timeDiff [ ' day ' ] / 365 ); // 按365天来算
30 return $timeDiff ;
31 }
32 ?>
用法:
$a=timeDiff("20060613161000","20040613150000");
print_r($a);
返回一个数组,可以用下标直接输出来年,月,周,日,小时,分钟,秒数之差
顶(0)
踩(0)
上一篇:PHP程序员最易犯10种错误
下一篇:php计算页面执行时间
- 最新评论