根据时间转换今天昨天刚刚

<?php

/**
 * 根据时间得到 (今天,昨天...)
 * @param $date 2017-01-01
 */
function getStrDate($the_time)
{
    date_default_timezone_set("Asia/Shanghai");   //设置时区
    $now_time = time();//当前时间
    $show_time = $the_time;
    $dur = $now_time - $show_time;//两个时间的差距
    if ($dur < 0) {
        return date("Y-m-d H:i:s",$the_time);
    } else {
        if ($dur < 60) {
            return '刚刚';
        } 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 date("Y-m-d H:i:s",$the_time);
                    }
                }
            }
        }
    }
}

/**
* 将时间戳转为形式如:几天前、几月前、几分前、几秒前 刚刚
* @param int $ctime 时间戳
* @return string
* @author Jacks(120041966@qq.com)
*/
function formatCtime($ctime) {
$time = time() - $ctime;
$today = strtotime(date('Y-m-d', time()));
$year = date('Y', time());
$cyear = date('Y', $ctime);
//1、1-5分钟内:刚刚
if($year == $cyear) {
if($time <= 300){
$str = '刚刚';
// 2、5-60分钟内:XX分钟前
}
else if($time <= 3600){
$str = ceil($time / 60) . "分钟前";
//3、1小时-今日结束前:今天
}
else if($ctime >= $today){
$str = intval($time / 3600). '小时前';
//4、昨天:昨天(如果用户是在半夜11点59分操作,操作完就到第二天了,也是显示昨天,而不是刚刚)
}
else {
$str = date('m-d H:i', $ctime);
}
}
else {
$str = date('Y-m-d', $ctime);
}
return $str;
}

?>
原文地址:https://www.cnblogs.com/jhy-ocean/p/7478706.html