简单好用PHP日期时间处理函数,返回类似【6小时8分钟前】的格式

16-12-20 15:44 字数 2286 阅读 4335 已编辑

如果你需要显示一篇的文章的发表时间,下面的函数可能会帮到你。

/**
 * Created by PhpStorm.
 * @author: saonian
 * @date: 2016/12/20
 * @time: 13:34
 */

$now_time = date('Y-m-d H:i:s');
$time1 = '2016-12-20 14:50:50';
$time2 = '2016-12-20 09:30:12';
$time3 = '2016-12-15 09:30:12';
$time4 = '2016-12-10 09:30:12';

echo "Now: ".date('Y-m-d H:i:s');//Now: 2016-12-20 15:38:30
var_dump(getTimeDiff($now_time));//刚刚
var_dump(getTimeDiff($time1));//47 分钟 40 秒前
var_dump(getTimeDiff($time2));//6 小时 8 分钟前
var_dump(getTimeDiff($time3));//5 天 6 小时前
var_dump(getTimeDiff($time4));//12/10 09:30

function getTimeDiff($time,$lang='cn'){
    $res_string = $time;
    if ($lang == 'cn'){
        $short = '刚刚';$ago = '前';$second = '秒';$minute = '分钟';$hour = '小时';$day = '天';
    }else{
        $short = 'one minute ago';$ago = 'ago';$second = 'sec';$minute = 'min';$hour = 'hour';$day = 'day';
    }
    if(!empty($time) && check_DT($time)){
        $time_diff = abs(intval(time() - strtotime($time)));
        if($time_diff < 1){
            $res_string = $short;
        }elseif ($time_diff < 60){//1分钟内
            $res_string = $time_diff . ' ' . $second . $ago;
        }elseif ($time_diff < 3600){//1小时内
            $min_part = floor($time_diff / 60);
            $min_part_s = $time_diff % 60;
            if(!$min_part_s){
                $res_string = $min_part . ' ' . $minute . $ago;
            }else{
                $res_string = $min_part . ' ' . $minute . ' ' . $min_part_s . ' ' . $second.$ago;
            }
        }elseif ($time_diff < 86400){//1天内
            $hour_part = floor($time_diff / 3600);//小时
            $min_part = floor(($time_diff % 3600) / 60);//剩余分钟
            if(!$min_part){
                $res_string = $hour_part . ' ' . $hour . $ago;
            }else{
                $res_string = $hour_part . ' ' . $hour. ' '  . $min_part . ' ' .$minute . $ago;
            }
        }elseif ($time_diff < 604800){//1周内
            $day_part = floor($time_diff / 86400);//天
            $hour_part = floor(($time_diff % 86400) / 3600);//剩余分钟
            if(!$hour_part){
                $res_string = $day_part . ' ' . $day . $ago;
            }else{
                $res_string = $day_part . ' ' . $day. ' '  . $hour_part . ' ' . $hour . $ago;
            }
        }else{//大于1周
            $res_string = sliceDT($time);
        }
    }
    return $res_string;
}

function check_DT($date_time,$format='Y-m-d H:i:s'){
    if(date($format, strtotime($date_time)) == $date_time){
        return true;
    }
    return false;
}

function sliceDT($date_time){
    $res_string = $date_time;
    if(check_DT($date_time)){
        $year = date('Y', strtotime($date_time));
        $month = date('m', strtotime($date_time));
        $day = date('d', strtotime($date_time));
        $hour = date('H', strtotime($date_time));
        $min = date('i', strtotime($date_time));
        $sec = date('s', strtotime($date_time));
        if(date('Y') == $year){
            if(date('Ymd') == $year.$month.$day){
                $res_string = $hour.':'.$min.':'.$sec;
            }else{
                $res_string = $month.'/'.$day.' '.$hour.':'.$min;
            }
        }else{
            $res_string = $year.'/'.$month.'/'.$day;
        }
    }
    return $res_string;
}
0人点赞>
关注 收藏 改进 举报
5 条评论
排序方式 时间 投票
香草大叔

@saonian 嗯嗯,了解。了解。

香草大叔

很好,支持楼主。[em_13]

Up骚年
回复1#: 多谢支持。
请登录后发表评论
站长 @ 十七度
文章
384
粉丝
23
喜欢
195
收藏
31
排名 : 1
访问 : 147.29万
私信