When you usually browse websites, you may often see comments on 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:

//友好化时间显示
function mdate($time)
  {
   $t=time()-$time;
      $f=array(
        '31536000'=> '年',
        '2592000' => '个月',
        '604800'  => '星期',
        '86400'   => '天',
        '3600'    => '小时',
        '60'      => '分钟',
        '1'       => '秒'
    );
    foreach ($f as $k=>$v){        
        if (0 !=$c=floor($t/(int)$k)){
            return $c.$v.'前';
        }
    }
  }

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.