i漂泊

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 4606|回复: 0

php处理日期格式为秒前、分前、小时前、昨天、前天

[复制链接]
TA的礼物信息
  • 收到:0
  • 送出:2
发表于 2013-7-16 19:20:18 | 显示全部楼层 |阅读模式
  1.     /**
  2.      * 将日期格式根据以下规律修改为不同显示样式
  3.      * 小于1分钟 则显示多少秒前
  4.      * 小于1小时,显示多少分钟前
  5.      * 一天内,显示多少小时前
  6.      * 3天内,显示前天22:23或昨天:12:23。
  7.      * 超过3天,则显示完整日期。
  8.      * @static
  9.      * @param  $sorce_date 数据源日期 unix时间戳
  10.      * @return void
  11.      */
  12.     public static function getDateStyle($sorce_date){

  13.         self::$nowTime = time();  //获取今天时间戳

  14. //        echo '数据源时间戳:'.$sorce_date . ' = '. date('Y-m-d H:i:s',$sorce_date);
  15. //        echo "\n 当前时间戳:". date('Y-m-d H:i:s',self::$nowTime)."\n";

  16.         $timeHtml = ''; //返回文字格式
  17.         $temp_time = 0;
  18.         switch($sorce_date){

  19.             //一分钟
  20.             case ($sorce_date+60)>=self::$nowTime:
  21.                 $temp_time = self::$nowTime-$sorce_date;
  22.                 $timeHtml = $temp_time ."秒前";
  23.                 break;

  24.             //小时
  25.             case ($sorce_date+3600)>=self::$nowTime:
  26.                 $temp_time = date('i',self::$nowTime-$sorce_date);
  27.                 $timeHtml = $temp_time ."分钟前";
  28.                 break;

  29.             //天
  30.             case ($sorce_date+3600*24)>=self::$nowTime:
  31.                 $temp_time = date('H',self::$nowTime)-date('H',$sorce_date);
  32.                 $timeHtml = $temp_time .'小时前';
  33.                 break;

  34.             //昨天
  35.             case ($sorce_date+3600*24*2)>=self::$nowTime:
  36.                 $temp_time = date('H:i',$sorce_date);
  37.                 $timeHtml = '昨天'.$temp_time ;
  38.                 break;

  39.             //前天
  40.             case ($sorce_date+3600*24*3)>=self::$nowTime:
  41.                 $temp_time  = date('H:i',$sorce_date);
  42.                 $timeHtml = '前天'.$temp_time ;
  43.                 break;

  44.             //3天前
  45.             case ($sorce_date+3600*24*4)>=self::$nowTime:
  46.                 $timeHtml = '3天前';
  47.                 break;

  48.             default:
  49.                 $timeHtml = date('Y-m-d',$sorce_date);
  50.                 break;

  51.         }
  52.         return $timeHtml;

  53.     }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|IPiaoBo Inc. ( 渝ICP备17002826号 )

GMT+8, 2024-5-5 18:00 , Processed in 0.073834 second(s), 55 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表