The remaining time refers to how many minutes or days it has been since an article was published. In many blog forums, you can see that the article was published 1 day ago. Below I will introduce two examples
Combine the two The string in date format is converted into a unix timestamp, and then subtracted to obtain the timestamp difference. Finally, judge the remaining time and generate a time format similar to (published 2 hours, 30 minutes and 20 seconds ago)
The code is as follows
代码如下 |
复制代码 |
public function gettime($time_s,$time_n){
$time_s = strtotime($time_s);
$time_n = strtotime($time_n);
$strtime = '';
$time = $time_n-$time_s;
if($time >= 86400){
return $strtime = date('Y-m-d H:i:s',$time_s);
}
if($time >= 3600){
$strtime .= intval($time/3600).'小时';
$time = $time % 3600;
}else{
$strtime .= '';
}
if($time >= 60){
$strtime .= intval($time/60).'分钟';
$time = $time % 60;
}else{
$strtime .= '';
}
if($time > 0){
$strtime .= intval($time).'秒前';
}else{
$strtime = "时间错误";
}
return $strtime;
}
|
|
Copy code
|
public function gettime($time_s,$time_n){
$time_s = strtotime($time_s);
$time_n = strtotime($time_n);
$strtime = '';
$time = $time_n-$time_s;
if($time >= 86400){
return $strtime = date('Y-m-d H:i:s',$time_s);
}
if($time >= 3600){
$strtime .= intval($time/3600).'hour';
$time = $time % 3600;
}else{
$strtime .= '';
}
if($time >= 60){
$strtime .= intval($time/60).'minutes';
$time = $time % 60;
}else{
$strtime .= '';
}
if($time > 0){
$strtime .= intval($time).'seconds ago';
}else{
$strtime = "Wrong time";
}
return $strtime;
}
First determine whether the value you want to subtract is greater than the number of seconds in a day, 86400 seconds. If it is greater, return the time queried from the original database
Then determine whether it is within 1 hour to one day, that is, 3600 seconds-86400 seconds. If it is within, return X hours. After getting the result, you need to use the remainder method to remove the hour part of the time, use % to take the remainder |
Then determine whether it is within 1 minute to one hour, that is, 60 seconds - 3600 seconds. If it is within, return X minutes. After getting the result, you need to use the remainder method to remove the minute part of the time, use % to take the remainder
Finally determine whether it is within 1 minute, that is, 0 seconds - 60 seconds. If it is within 1 minute, return X minutes and seconds
Note: The results obtained above are all connected using .=. In this way, we finally get an overall time.
http://www.bkjia.com/PHPjc/632724.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/632724.htmlThe remaining time refers to the minutes or days since an article was published. This is used in many blogs. Everyone in the forum has seen the article posted 1 day ago. Below I will introduce two examples...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn