首页 » PHP 技术 » 浏览内容

PHP计算两个时间之差的函数(年,月,周,日,小时,分钟,秒数)


2006-09-07 18:17:43 8,999 views 0 发表评论 字体: 作者:C.K.
标签: 源程序

From:http://www.im286.com/viewthread.php?tid=1586379&extra=&highlight=php&page=1

PHP的时间函数并不是太丰富,所以今天刚刚写了一个非常方便的计算两个时间截的年,月,周,日,小时,分钟,秒数之差~~!!

用法:
$a=timeDiff(“20060613161000″,”20040613150000”);
print_r($a);

PS:返回一个数组的,可以用下标直接输出来年,月,周,日,小时,分钟,秒数之差

// 时间格式为YYYYMMDDHHmmss
function timeDiff($aTime,$bTime)
{
//分割第一个时间
$ayear = substr($aTime,0,4);
$amonth = substr($aTime,4,2);
$aday = substr($aTime,6,2);
$ahour = substr($aTime,8,2);
$aminute = substr($aTime,10,2);
$asecond = substr($aTime,12,2);
//分割第二个时间
$byear = substr($bTime,0,4);
$bmonth = substr($bTime,4,2);
$bday = substr($bTime,6,2);
$bhour = substr($bTime,8,2);
$bminute = substr($bTime,10,2);
$bsecond = substr($bTime,12,2);
//生成时间戳
$a = mktime($ahour,$aminute,$asecond,$amonth,$aday,$ayear);
$b = mktime($bhour,$bminute,$bsecond,$bmonth,$bday,$byear);
$timeDiff['second'] = $a-$b;
//采用了四舍五入,可以修改
$timeDiff['mintue'] = round($timeDiff['second']/60);
$timeDiff['hour'] = round($timeDiff['mintue']/60);
$timeDiff['day'] = round($timeDiff['hour']/24);
$timeDiff['week'] = round($timeDiff['day']/7);
$timeDiff['month'] = round($timeDiff['day']/30); //按30天来算
$timeDiff['year'] = round($timeDiff['day']/365); //按365天来算
return $timeDiff;
}
?>

评论 0 条回应 (访客:0 条, 博主:0 条) 发表

  1. 暂无评论,快抢沙发吧。

发表评论

  • 插入图像

要发表评论,您必须先登录