今天开发Web2.0站点时写的一个返回日期时间差的函数分享一下
//取得日期时间差
static function getTimeDifference($time_two){
$time_one = date("Y-m-d H:i:s");
$stamp_one=strtotime($time_one);
$stamp_two=strtotime($time_two);
$time_diff=abs($stamp_one-$stamp_two);
$diff_time = array();
$diff_time["year"] = 0;
if($time_diff > 31536000) //一年31536000秒
$diff_time["year"] = floor($time_diff / 31536000);
$time_diff = $time_diff - $diff_time["year"] * 31536000;
$diff_time["month"] = 0;
if($time_diff > 2592000) //一月2592000秒
$diff_time["month"] = floor($time_diff / 2592000);
$time_diff = $time_diff - $diff_time["month"] * 2592000;
$diff_time["day"] = 0;
if($time_diff > 86400) //一天86400秒
$diff_time["day"] = floor($time_diff / 86400);
$time_diff = $time_diff - $diff_time["day"] * 86400;
$diff_time["hour"] = 0;
if($time_diff > 3600) //一小时3600秒
$diff_time["hour"] = floor($time_diff / 3600);
$time_diff = $time_diff - $diff_time["hour"] * 3600;
$diff_time["minute"] = 0;
if($time_diff > 60) //一分60秒
$diff_time["minute"] = floor($time_diff / 60);
$time_diff = $time_diff - $diff_time["minute"] * 60;
if(intval($diff_time["month"]) > 0){
return $diff_time["month"]."个月前";
}
else if(intval($diff_time["day"]) > 0){
return $diff_time["day"]."天前";
}
else if(intval($diff_time["hour"]) > 0){
return $diff_time["hour"]."小时前";
}
else{
return $diff_time["minute"]."分钟前";
}
}
本文转自dotfun 51CTO博客,原文链接:http://blog.51cto.com/dotfun/286016