How php timestamp formatting displays friendly time functions
This article introduces php timestamp format how to display friendly time function, the content is very detailed, interested friends can refer to reference, hope to be helpful to everyone.
The general idea is as follows:
If it's a new year and more than 3 days, it's displayed as a specific time.
If it's today's
If it's within a minute, it shows seconds ago.
If it's within an hour, it shows minutes ago.
If it's today and more than an hour, it's hours ago.
If it was yesterday, what time was it yesterday?
If it's the day before yesterday, what time is it?
If it is longer than three days (no new year), it will be displayed as the month and day.
According to the above ideas, it is not difficult to write the implementation code:
The implementation code is as follows:
The copy 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);
//Days of distance. This method is not necessarily accurate beyond 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]);
//Seconds of distance
$secs=$now-$time;
if($todayArr[0]-$dayArr[0]>0 && $days>3){//New Year and more than 3 days
return date('Y-m-d',$time);
}else{
if($days