PHP中"几多时间之前的"做法
程序员文章站
2024-01-24 20:41:05
...
PHP中"多少时间之前的"做法
经常看到,有的视频站上,都显示某个视频,是多少天之前,多少分钟之前,多少年之前,
其实实现起来不麻烦,下面是函数:
include_once('timeago.php');
$cur_time1 ="1291684422";//UNIX时间,统一用
echo time_ago($cur_time1) ;
?>
timeago.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
$time="$seconds seconds ago";
//Minutes
}else if($minutes
if($minutes == 1){
$time="one minute ago";
}else{
$time="$minutes minutes ago";
}
//Hours
}else if($hours
if($hours == 1){
$time="one hour ago";
}else{
$time="$hours hours ago";
}
//Days
}else if($days
if($days == 1){
$time="one day ago";
}else{
$time="$days days ago";
}
//Weeks
}else if($weeks
if($weeks == 1){
$time="one week ago";
}else{
$time="$weeks weeks ago";
}
//Months
}else if($months
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年是没复数的表示上的分别的,所以上面的代码自己适当改下就可以用了
经常看到,有的视频站上,都显示某个视频,是多少天之前,多少分钟之前,多少年之前,
其实实现起来不麻烦,下面是函数:
include_once('timeago.php');
$cur_time1 ="1291684422";//UNIX时间,统一用
echo time_ago($cur_time1) ;
?>
timeago.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
$time="$seconds seconds ago";
//Minutes
}else if($minutes
if($minutes == 1){
$time="one minute ago";
}else{
$time="$minutes minutes ago";
}
//Hours
}else if($hours
if($hours == 1){
$time="one hour ago";
}else{
$time="$hours hours ago";
}
//Days
}else if($days
if($days == 1){
$time="one day ago";
}else{
$time="$days days ago";
}
//Weeks
}else if($weeks
if($weeks == 1){
$time="one week ago";
}else{
$time="$weeks weeks ago";
}
//Months
}else if($months
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年是没复数的表示上的分别的,所以上面的代码自己适当改下就可以用了
相关文章
相关视频
下一篇: mysql连接慢的问题_MySQL