Php format timestamp display friendly time, php format. Php format timestamp display friendly time, php format in the project time is displayed as 2014-10-2010:22 looks very dull. Websites such as Weibo and QQ space are usually displayed a few seconds ago. php formatted timestamp displays friendly time, and php formatted
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 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]); // The number of seconds from the distance $ secs = $ now-$ time; if ($ todayArr [0]-$ dayArr [0]> 0 & $ days> 3) {// return date ('Y-m-D', $ time) beyond 3 days across the year;} 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 {// return date ('m month d ', $ time) three days ago );}}}
For your reference only. You are welcome to criticize and correct or provide better methods. Blog: http://www.cnblogs.com/dragondean/
The php date function can format the timestamp into 2012-5-3.
Date ('Y-n-J', time (); // The time () function generates the timestamp of the current time. you can replace it with your own timestamp.
Reference: cn2.php.net/manual/zh/function.date.php
PHP time formatting
The simplest truncation field substr ("02:10:45", 10)
-------------------
Haha, because the time you save is not a timestamp, but a formatted time, there is no need to format it again; if you really want to format it again, then convert strtotime () to a timestamp, and then date ("H_ I _s ")......
Try replacing single quotes with double quotes
The time displayed for the period in the project is, which seems very dull. Websites such as Weibo and QQ space are usually displayed a few seconds ago ,...