PHP formatted timestamp display friendly time implementation ideas and code_PHP tutorial

WBOY
Release: 2016-07-13 10:16:08
Original
925 people have browsed it

PHP formatted timestamp display friendly time implementation ideas and codes

In the project, the time 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 greater than 3 days, it will be displayed as the specific time

If it were today

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

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:

//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 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('m month d',$time);

 }

 }

 }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/899487.htmlTechArticlephp formatted timestamp displays friendly time implementation ideas and codes. The time in the project is always displayed as 2014-10- 20 10:22 seems very dull. On Weibo, QQ space and other websites, it usually displays for a few seconds...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template