Php formatted timestamp display friendly time implementation ideas and code _ PHP Tutorial

Source: Internet
Author: User
Php formatted timestamp to display friendly time implementation ideas and code. Php formatted the timestamp to display friendly time implementation ideas and codes. the time displayed in the project is 2014-10-, which is very dull. On Weibo, QQ, and other websites, it is usually displayed as several seconds. php formatted timestamp shows friendly time implementation ideas and code.

The time displayed in the project is, which seems dull. 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:

// 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 );

}

}

}

The time displayed for the period in the project is, which seems very dull. Weibo, QQ space, and other websites are usually displayed as several seconds...

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.