HTML/CSS

PHP 格式化时间显示刚刚、几分钟前、昨天、前天

阿里云

PHP 把时间转换为友好时间段,如刚刚、几分钟前、几小时前、几天前的简单函数代码。通过把时间格式转换为时间戳,并把当前的时间戳减去之前时间的时间戳,相减后的时间戳除以相对应的秒数得到刚刚、几分钟前、几小时前、几天前的展示,需要的朋友可以参考下:

  1. //时间格式化(时间戳)
  2. function uc_time_ago($ptime) {
  3. 	date_default_timezone_set('PRC');
  4. 	//$ptime = strtotime($ptime);
  5. 	$etime = time() - $ptime;
  6. 	switch ($etime){
  7. 		case $etime <= 60:
  8. 			$msg = '刚刚';
  9. 			break;
  10. 		case $etime > 60 && $etime <= 60 * 60:
  11. 			$msg = floor($etime / 60) . ' 分钟前';
  12. 			break;
  13. 		case $etime > 60 * 60 && $etime <= 24 * 60 * 60:
  14. 			$msg = date('Ymd',$ptime)==date('Ymd',time()) ? '今天 '.date('H:i',$ptime) : '昨天 '.date('H:i',$ptime);
  15. 			break;
  16. 		case $etime > 24 * 60 * 60 && $etime <= 2 * 24 * 60 * 60:
  17. 			$msg = date('Ymd',$ptime)+1==date('Ymd',time()) ? '昨天 '.date('H:i',$ptime) : '前天 '.date('H:i',$ptime);
  18. 			break;
  19. 		case $etime > 2 * 24 * 60 * 60 && $etime <= 12 * 30 * 24 * 60 * 60:
  20. 			$msg = date('Y',$ptime)==date('Y',time()) ? date('m-d H:i',$ptime) : date('Y-m-d H:i',$ptime);
  21. 			break;
  22. 		default: $msg = date('Y-m-d H:i',$ptime);
  23. 	}
  24. 	return $msg;
  25. }
也想出现在这里?联系我们
创客主机

实例 1:

  1. <?php
  2. header("Content-type: text/HTML; charset=utf8");
  3. date_default_timezone_set("Asia/Shanghai");   //设置时区
  4. function time_tran($the_time) {
  5.     $now_time = date("Y-m-d H:i:s", time());
  6. 	//echo $now_time;
  7.     $now_time = strtotime($now_time);
  8.     $show_time = strtotime($the_time);
  9.     $dur = $now_time - $show_time;
  10.     if ($dur < 0) {
  11.         return $the_time;
  12.     } else {
  13.         if ($dur < 60) {
  14.             return $dur . '秒前';
  15.         } else {
  16.             if ($dur < 3600) {
  17.                 return floor($dur / 60) . '分钟前';
  18.             } else {
  19.                 if ($dur < 86400) {
  20.                     return floor($dur / 3600) . '小时前';
  21.                 } else {
  22.                     if ($dur < 259200) {//3天内
  23.                         return floor($dur / 86400) . '天前';
  24.                     } else {
  25.                         return $the_time;
  26.                     }
  27.                 }
  28.             }
  29.         }
  30.     }
  31. }
  32.  
  33.  
  34. echo time_tran("2014-7-8 19:22:01");
  35. ?>
  36. <h2>实例2:</h2>
  37. <pre lang="PHP" line="1" escaped="true" ><?php
  38. function time_tranx($the_time){
  39.    $now_time = date("Y-m-d H:i:s",time()+8*60*60);
  40.    $now_time = strtotime($now_time);
  41.    $show_time = strtotime($the_time);
  42.    $dur = $now_time - $show_time;
  43.    if($dur < 0){
  44.     	return $the_time;
  45.    }else{
  46. 		if($dur < 60){
  47. 		 return $dur.'秒前';
  48. 		}else{
  49. 			 if($dur < 3600){
  50. 			  return floor($dur/60).'分钟前';
  51. 			 }else{
  52. 				  if($dur < 86400){
  53. 					 return floor($dur/3600).'小时前';
  54. 				  }else{
  55. 					   if($dur < 259200){ //3天内
  56. 							return floor($dur/86400).'天前';
  57. 					   }else{
  58. 							return $the_time;
  59. 					   }
  60. 				  }
  61. 			}
  62. 		}
  63.    }
  64. }
  65. echo time_tranx("2014-7-8 19:22:01");
  66. ?>

实例 3:

  1. <?php
  2. function format_date($time){
  3.     $t=time()-$time;
  4. <span style="white-space:pre">	</span>//echo time();
  5.     $f=array(
  6.         '31536000'=>'年',
  7.         '2592000'=>'个月',
  8.         '604800'=>'星期',
  9.         '86400'=>'天',
  10.         '3600'=>'小时',
  11.         '60'=>'分钟',
  12.         '1'=>'秒'
  13.     );
  14.     foreach ($f as $k=>$v)    {
  15.         if (0 !=$c=floor($t/(int)$k)) {
  16.             return $c.$v.'前';
  17.         }
  18.     }
  19. }
  20. echo format_date("1404600000");
  21. ?>

实例 4:

  1. <?php
  2. function formatTime($date) {
  3.     $str = '';
  4.     $timer = strtotime($date);
  5.     $diff = $_SERVER['REQUEST_TIME'] - $timer;
  6.     $day = floor($diff / 86400);
  7.     $free = $diff % 86400;
  8.     if($day > 0) {
  9.         return $day."天前";
  10.     }else{
  11.         if($free>0){
  12.             $hour = floor($free / 3600);
  13.             $free = $free % 3600;
  14.                 if($hour>0){
  15.                     return $hour."小时前";
  16.                 }else{
  17.                     if($free>0){
  18.                         $min = floor($free / 60);
  19.                         $free = $free % 60;
  20.                         if($min>0){
  21.                             return $min."分钟前";
  22.                         }else{
  23.                             if($free>0){
  24.                                 return $free."秒前";
  25.                             }else{
  26.                                 return '刚刚';
  27.                             }
  28.                        }
  29.                     }else{
  30.                         return '刚刚';
  31.                     }
  32.                }
  33.        }else{
  34.            return '刚刚';
  35.        }
  36.     }
  37. }
  38. echo formatTime("2014-7-8 19:22:01");
  39. ?>

类的实现

  1. <?php
  2. /*
  3.  * author: china_skag
  4.  * time: 2014-07-08
  5.  * 发博时间计算(年,月,日,时,分,秒)
  6.  * $createtime 可以是当前时间
  7.  * $gettime 你要传进来的时间
  8.  */
  9. class Mygettime{
  10.         function  __construct($createtime,$gettime) {
  11.             $this->createtime = $createtime;
  12.             $this->gettime = $gettime;
  13.     }
  14.     function getSeconds()
  15.     {
  16.             return $this->createtime-$this->gettime;
  17.         }
  18.     function getMinutes()
  19.        {
  20.        return ($this->createtime-$this->gettime)/(60);
  21.        }
  22.       function getHours()
  23.        {
  24.        return ($this->createtime-$this->gettime)/(60*60);
  25.        }
  26.       function getDay()
  27.        {
  28.         return ($this->createtime-$this->gettime)/(60*60*24);
  29.        }
  30.       function getMonth()
  31.        {
  32.         return ($this->createtime-$this->gettime)/(60*60*24*30);
  33.        }
  34.        function getYear()
  35.        {
  36.         return ($this->createtime-$this->gettime)/(60*60*24*30*12);
  37.        }
  38.        function index()
  39.        {
  40.             if($this->getYear() > 1)
  41.             {
  42.                  if($this->getYear() > 2)
  43.                     {
  44.                         return date("Y-m-d",$this->gettime);
  45.                         exit();
  46.                     }
  47.                 return intval($this->getYear())." 年前";
  48.                 exit();
  49.             }
  50.              if($this->getMonth() > 1)
  51.             {
  52.                 return intval($this->getMonth())." 月前";
  53.                 exit();
  54.             }
  55.              if($this->getDay() > 1)
  56.             {
  57.                 return intval($this->getDay())." 天前";
  58.                 exit();
  59.             }
  60.              if($this->getHours() > 1)
  61.             {
  62.                 return intval($this->getHours())." 小时前";
  63.                 exit();
  64.             }
  65.              if($this->getMinutes() > 1)
  66.             {
  67.                 return intval($this->getMinutes())." 分钟前";
  68.                 exit();
  69.             }
  70.            if($this->getSeconds() > 1)
  71.             {
  72.                 return intval($this->getSeconds()-1)." 秒前";
  73.                 exit();
  74.             }
  75.        }
  76.   }
  77. //类的使用实例
  78. /*
  79.  *
  80.  * 调用类输出方式
  81.  *
  82.  * $a = new Mygettime(time(),strtotime('-25 month'));
  83.  * echo iconv('utf-8', 'gb2312', $a->index())?iconv('utf-8', 'gb2312', $a->index()):iconv('utf-8', 'gb2312', '当前');
  84.  *
  85.  */

PHP 格式化时间显示刚刚、几分钟前、昨天、前天

已有 485 人购买
查看演示升级 VIP立刻购买

收藏
(1)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!