Detailed explanation of php formatted timestamp graphic and text

墨辰丷
Release: 2023-03-28 10:58:01
Original
2087 people have browsed it

Generally, Unix timestamps are used to store time in mysql database. So what if it is more friendly when we display the time? Today I will share with you 3 encapsulated formatting functions. Friends in need can refer to it. Next

Generally, articles with a relatively new display time in CMS need to be displayed a few minutes ago, a few days ago, but generally the database records are timestamps (at least I am used to this), so a conversion is needed In the process, I encapsulated two pieces of code based on the information on the Internet and my own modifications

The following is the encapsulated method


function formatTime($time) {
    $time = (int) substr($time, 0, 10);
    $int = time() - $time;
    $str = '';
    if ($int <= 2){
      $str = sprintf(&#39;刚刚&#39;, $int);
    }elseif ($int < 60){
      $str = sprintf(&#39;%d秒前&#39;, $int);
    }elseif ($int < 3600){
      $str = sprintf(&#39;%d分钟前&#39;, floor($int / 60));
    }elseif ($int < 86400){
      $str = sprintf(&#39;%d小时前&#39;, floor($int / 3600));
    }elseif ($int < 2592000){
      $str = sprintf(&#39;%d天前&#39;, floor($int / 86400));
    }else{
      $str = date(&#39;Y-m-d H:i:s&#39;, $time);
    }
    return $str;
  }
Copy after login


Or more detailed


  function formatTime($time)
  {
    $text = &#39;&#39;;
    $time = intval($time);
    $ctime = time();
    $t = $ctime - $time; //时间差
    if ($t < 0) {
      return date(&#39;Y-m-d&#39;, $time);
    }
    ;
    $y = date(&#39;Y&#39;, $ctime) - date(&#39;Y&#39;, $time);//是否跨年
    switch ($t) {
      case $t == 0:
        $text = &#39;刚刚&#39;;
        break;
      case $t < 60://一分钟内
        $text = $t . &#39;秒前&#39;;
        break;
      case $t < 3600://一小时内
        $text = floor($t / 60) . &#39;分钟前&#39;;
        break;
      case $t < 86400://一天内
        $text = floor($t / 3600) . &#39;小时前&#39;; // 一天内
        break;
      case $t < 2592000://30天内
        if($time > strtotime(date(&#39;Ymd&#39;,strtotime("-1 day")))) {
          $text = &#39;昨天&#39;;
        } elseif($time > strtotime(date(&#39;Ymd&#39;,strtotime("-2 days")))) {
          $text = &#39;前天&#39;;
        } else {
          $text = floor($t / 86400) . &#39;天前&#39;;
        }
        break;
      case $t < 31536000 && $y == 0://一年内 不跨年
        $m = date(&#39;m&#39;, $ctime) - date(&#39;m&#39;, $time) -1;

        if($m == 0) {
          $text = floor($t / 86400) . &#39;天前&#39;;
        } else {
          $text = $m . &#39;个月前&#39;;
        }
        break;
      case $t < 31536000 && $y > 0://一年内 跨年
        $text = (11 - date(&#39;m&#39;, $time) + date(&#39;m&#39;, $ctime)) . &#39;个月前&#39;;
        break;
      default:
        $text = (date(&#39;Y&#39;, $ctime) - date(&#39;Y&#39;, $time)) . &#39;年前&#39;;
        break;
    }

    return $text;
  }
Copy after login


Share a similar


//格式化友好显示时间
function formatTime($time){
	$now=time();
	$day=date(&#39;Y-m-d&#39;,$time);
	$today=date(&#39;Y-m-d&#39;);
	
	$dayArr=explode(&#39;-&#39;,$day);
	$todayArr=explode(&#39;-&#39;,$today);
	
	//距离的天数,这种方法超过30天则不一定准确,但是30天内是准确的,因为一个月可能是30天也可能是31天
	$days=($todayArr[0]-$dayArr[0])*365+(($todayArr[1]-$dayArr[1])*30)+($todayArr[2]-$dayArr[2]);
	//距离的秒数
	$secs=$now-$time;
	
	if($todayArr[0]-$dayArr[0]>0 && $days>3){//跨年且超过3天
		return date(&#39;Y-m-d&#39;,$time);
	}else{
	
		if($days<1){//今天
			if($secs<60)return $secs.&#39;秒前&#39;;
			elseif($secs<3600)return floor($secs/60)."分钟前";
			else return floor($secs/3600)."小时前";
		}else if($days<2){//昨天
			$hour=date(&#39;h&#39;,$time);
			return "昨天".$hour.&#39;点&#39;;
		}elseif($days<3){//前天
			$hour=date(&#39;h&#39;,$time);
			return "前天".$hour.&#39;点&#39;;
		}else{//三天前
			return date(&#39;m月d号&#39;,$time);
		}
	}
}
Copy after login




The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

php format timestamp displays friendly time, php formatting

mysql time formatting_MySQL

thinkPHP formatting time problem


The above is the detailed content of Detailed explanation of php formatted timestamp graphic and text. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!