PHP formatted timestamp shows friendly time to implement ideas and code

Source: Internet
Author: User
Tags explode

All the time in the project shows the 2014-10-20 10:22 looks very stiff. In micro-blog, QQ space and other sites will usually show a few seconds ago, a few minutes ago, hours ago, such as easy to read time, we call it a friendly time format. So how to use PHP to achieve it?

The general idea is as follows:

If it is a span year and is greater than 3 days, it is displayed as a specific time

If it's Today's

If it's in a minute, then a few seconds ago,

If it's within an hour, a few minutes ago.

If it is the same day and is greater than one hour, it is displayed a few hours ago

If it was yesterday, it would show the time yesterday.

If the day before yesterday, show the day before yesterday

If it is greater than three days (no span years), it will appear for a few months.

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 distance of the number of days, this method more than 30 days is not necessarily accurate, but 30 days is accurate, because one months may be 30 days or 31 days

$days = ($TODAYARR [0]-$dayArr [0]) *365+ (($todayArr [1]-$DAYARR [1]) *30) + ($TODAYARR [2]-$DAYARR [2]);

Number of seconds in distance

$secs = $now-$time;

if ($todayArr [0]-$dayArr [0]>0 && $days >3) {//span 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);

}

}

}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.