Home >Backend Development >PHP Tutorial >PHP timestamp formatting display friendly time function sharing, PHP function sharing_PHP tutorial
The time in the project 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 more than 3 days, it will be displayed as the specific time
If it’s today
If it is within one minute, it will show how many seconds ago
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:
For reference only, criticisms and corrections or better methods are welcome.
date('Y-n-j', time()); // The time() function generates a timestamp of the current time, which can be replaced with your own timestamp
Reference: cn2.php .net/manual/zh/function.date.php
time() Get the current timestamp
strtotime() Convert to timestamp
date('Y-m-d H:i:s',time()) Convert timestamp to time