经常看到,有的视频站上,都显示某个视频,是多少天之前,多少分钟之前,多少年之前,
其实实现起来不麻烦,下面是函数:
<?PHP
include_once('timeago.php');
$cur_time1 ="1291684422";//UNIX时间,统一用
echo time_ago($cur_time1) ;
?>
timeago.php
<?PHP
function time_ago($cur_time){
$time_ = time() - $cur_time;
$seconds =$time_;
$minutes = round($time_ / 60);
$hours = round($time_ / 3600);
$days = round($time_ / 86400);
$weeks = round($time_ / 604800);
$months = round($time_ / 2419200);
$years = round($time_ / 29030400);
//Seconds
if($seconds <= 60){
$time="$seconds seconds ago";
//Minutes
}else if($minutes <= 60){
if($minutes == 1){
$time="one minute ago";
}else{
$time="$minutes minutes ago";
}
//Hours
}else if($hours <= 24){
if($hours == 1){
$time="one hour ago";
}else{
$time="$hours hours ago";
}
//Days
}else if($days <= 7){
if($days == 1){
$time="one day ago";
}else{
$time="$days days ago";
}
//Weeks
}else if($weeks <= 4){
if($weeks == 1){
$time="one week ago";
}else{
$time="$weeks weeks ago";
}
//Months
}else if($months <= 12){
if($months == 1){
$time="one month ago";
}else{
$time="$months months ago";
}
//Years
}else{
if($year == 1){
$time="one year ago";
}else{
$time="$year years ago";
}
}
return $time;
}
?>
注意,由于英文跟中文的语法的习惯,中文的话,1年和2年是没复数的表示上的分别的,所以上面的代码自己适当改下就可以用了