Php timestamp format display friendly time function sharing

Source: Internet
Author: User
In some Weibo systems, the time is usually displayed before the current time, such as: just five seconds ago, five hours ago, five days ago .. this display is very friendly, so how can we use php to implement it? this is the content discussed in this article, for more information, see <g id = "1"> Project </g>. Websites such as Weibo and QQ space are usually displayed in a friendly time format that is a few seconds ago, a few minutes ago, or a few hours ago. So how can we implement it using php?

The general idea is as follows:

If it is a new year and more than 3 days, it is displayed as a specific time

If it is today's

If it is within one minute, it is displayed a few seconds ago.

If it is within one hour, it will be displayed several minutes ago

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

If it was yesterday, it is displayed as the time of yesterday

If it is the day before yesterday, it is displayed as the day before yesterday

If it is more than three days (not cross-year), it is displayed as the number of months

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

The implementation code is as follows:

The code is as follows:


// Format the 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 is not necessarily accurate if it is more than 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]);
// Number of seconds
$ Secs = $ now-$ time;

If ($ todayArr [0]-$ dayArr [0]> 0 & $ days> 3) {// cross-year period 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-d ', $ time );
}
}
}

For your reference only. You are welcome to criticize and correct or provide better methods.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.