• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
车车大人
博客园    首页    新随笔    联系   管理     

php将时间戳转换成几小时前的格式封装

方法1:

/**
 * 个性化日期显示
 * @static
 * @access public
 * @param datetime $times 日期
 * @return string 返回大致日期
 * @example 示例 ueTime('')
 */
function ueTime($times) {
    if ($times == '' || $times == 0) {
        return false;
    }
    //完整时间戳
    $strtotime = is_int($times) ? $times : strtotime($times);
    $times_day = date('Y-m-d', $strtotime);
    $times_day_strtotime = strtotime($times_day);

    //今天
    $nowdate_str = strtotime(date('Y-m-d'));

    //精确的时间间隔(秒)
    $interval = time() - $strtotime;

    //今天的
    if ($times_day_strtotime == $nowdate_str) {

        //小于一分钟
        if ($interval < 60) {
            $pct = sprintf("%d秒前", $interval);
        }
        //小于1小时
        elseif ($interval < 3600) {
            $pct = sprintf("%d分钟前", ceil($interval / 60));
        } else {
            $pct = sprintf("%d小时前", floor($interval / 3600));
        }
    }
    //昨天的
    elseif ($times_day_strtotime == strtotime(date('Y-m-d', strtotime('-1 days')))) {
        $pct = '昨天' . date('H:i', $strtotime);
    }
    //前天的
    elseif ($times_day_strtotime == strtotime(date('Y-m-d', strtotime('-2 days')))) {
        $pct = '前天' . date('H:i', $strtotime);
    }
    //一个月以内
    elseif ($interval < (3600 * 24 * 30)) {
        $pct = date('m月d日', $strtotime);
    }
    //一年以内
    elseif ($interval < (3600 * 24 * 365)) {
        $pct = date('m月d日', $strtotime);
    }
    //一年以上
    else {
        $pct = date('Y年m月d日', $strtotime);
    }
    return $pct;
}

 

 

方法2:

<?php  
header("Content-type: text/html; charset=utf8");  
date_default_timezone_set("Asia/Shanghai");   //设置时区  
function time_tran($the_time) {  
    $now_time = date("Y-m-d H:i:s", time());  
    //echo $now_time;  
    $now_time = strtotime($now_time);  
    $show_time = strtotime($the_time);  
    $dur = $now_time - $show_time;  
    if ($dur < 0) {  
        return $the_time;  
    } else {  
        if ($dur < 60) {  
            return $dur . '秒前';  
        } else {  
            if ($dur < 3600) {  
                return floor($dur / 60) . '分钟前';  
            } else {  
                if ($dur < 86400) {  
                    return floor($dur / 3600) . '小时前';  
                } else {  
                    if ($dur < 259200) {//3天内  
                        return floor($dur / 86400) . '天前';  
                    } else {  
                        return $the_time;  
                    }  
                }  
            }  
        }  
    }  
}  
  
  
echo time_tran("2014-7-8 19:22:01");  
?>  

 

方法3:

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

 

通往牛逼的路上,在意的只有远方!
posted @ 2017-03-10 22:55  车车大人  阅读(575)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3