When you usually browse the website, you may often see the comments of many websites showing just now, a few minutes ago, a few hours ago, etc.
As you can see, time display like this will make you feel more friendly and intimate. If the date is displayed like 2017/10/19, it seems to be very stiff. Today I will teach you how to implement friendly time display in Yiyou CMS. Please read below.
1. Find the root directory extend/function.php, we create a custom function method at the end of the file, the method is named mdate, and write the logic code:
//Friendly time display
function mdate($time)
{
$t=time()-$time;
$f=array(
'31536000'=> 'Year',
‘2592000’ => ‘months’,
'604800' => 'Week',
'86400' => 'day',
'3600' => 'hour',
'60' => 'minutes',
'1' => 'second'
);
foreach ($f as $k=>$v){
If (0 !=$c=floor($t/(int)$k)){
return $c.$v.'front';
}
}
}
2. Yiyou CMS template calling method: {$field.add_time|mdate}; ThinkPHP calling method: {$vo.date|mdate}.
It should be noted that the time passed in $time in the custom method must be a timestamp, not a date and time.