Home >Backend Development >PHP Tutorial >PHP timestamp formatting display friendly time function sharing, PHP function sharing_PHP tutorial

PHP timestamp formatting display friendly time function sharing, PHP function sharing_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:16:291248browse

php timestamp formatting display friendly time function sharing, php function sharing

The time in the project is always displayed as 2014-10-20 10:22, which seems very dull. On websites such as Weibo and QQ Space, the time is usually displayed as a few seconds ago, a few minutes ago, a few hours ago, etc. that are easy to read. We call this a friendly time format. So how to implement it using php?

The general idea is as follows:

If it is a New Year's Eve and it is more than 3 days, it will be displayed as the specific time

If it’s today

If it is within one minute, it will show how many seconds ago

If it is within one hour, it will display a few minutes ago

If it is the current day and is greater than one hour, it will be displayed as a few hours ago

If it is yesterday, the time will be displayed as yesterday

If it is the day before yesterday, it will display the time the day before yesterday

If it is more than three days (no New Year span), it will display the day of the month

Based on the above ideas, it is not difficult to write the implementation code:

The implementation code is as follows:

Copy code The code is as follows:

//Format friendly display time
function formatTime($time){
$now=time();
$day=date('Y-m-d',$time);
$today=date('Y-m-d');
 
$dayArr=explode('-',$day);
$todayArr=explode('-',$today);
 
//The number of days in the distance, this method may not be accurate if it exceeds 30 days, but it is accurate within 30 days, because a month may be 30 days or 31 days
$days=($todayArr[0]-$dayArr[0])*365+(($todayArr[1]-$dayArr[1])*30)+($todayArr[2]-$dayArr[2]) ;
//Distance in seconds
$secs=$now-$time;
 
If($todayArr[0]-$dayArr[0]>0 && $days>3){//Crossing the year and more than 3 days
         return date('Y-m-d',$time);
}else{
 
If($days<1){//Today
If($secs<60)return $secs.'seconds ago';
               elseif($secs<3600)return floor($secs/60)."Minutes ago";
                 else return floor($secs/3600)."hours ago";
           }else if($days<2){//Yesterday
              $hour=date('h',$time);
              return "yesterday".$hour.'point';
           }elseif($days<3){//The day before yesterday
              $hour=date('h',$time);
              return "The day before yesterday".$hour.'point';
         }else{//Three days ago
             return date('mth day of month',$time);
}
}
}

For reference only, criticisms and corrections or better methods are welcome.

php date function can format the timestamp into 2012-5-3

date('Y-n-j', time()); // The time() function generates a timestamp of the current time, which can be replaced with your own timestamp

Reference: cn2.php .net/manual/zh/function.date.php

What is the php timestamp function?

time() Get the current timestamp
strtotime() Convert to timestamp
date('Y-m-d H:i:s',time()) Convert timestamp to time

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/897695.htmlTechArticlePHP timestamp formatting displays friendly time function sharing. The time of php function sharing in the project will always be displayed as 2014- 10-20 10:22 seems very dull. Usually displayed on Weibo, QQ space and other websites...
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