ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# 时间戳友好格式化 ``` /** * 格式化时间戳 (刚刚,1分钟前)... * * @param [type] $posttime 时间戳 * @return string */ function time_ago($posttime) { //当前时间的时间戳 $nowtimes = time(); //相差时间戳 $counttime = $nowtimes - $posttime; //进行时间转换 if ($counttime <= 60) { return '刚刚'; } else if ($counttime > 60 && $counttime <= 120) { return '1分钟前'; } else if ($counttime > 120 && $counttime <= 180) { return '2分钟前'; } else if ($counttime > 180 && $counttime < 3600) { return intval(($counttime / 60)) . '分钟前'; } else if ($counttime >= 3600 && $counttime < 3600 * 24) { return intval(($counttime / 3600)) . '小时前'; } else if ($counttime >= 3600 * 24 && $counttime < 3600 * 24 * 2) { return '昨天'; } else if ($counttime >= 3600 * 24 * 2 && $counttime < 3600 * 24 * 3) { return '前天'; } else if ($counttime >= 3600 * 24 * 3 && $counttime <= 3600 * 24 * 7) { return intval(($counttime / (3600 * 24))) . '天前'; } else if ($counttime >= 3600 * 24 * 7 && $counttime <= 3600 * 24 * 30) { return intval(($counttime / (3600 * 24 * 7))) . '周前'; } else if ($counttime >= 3600 * 24 * 30 && $counttime <= 3600 * 24 * 365) { return intval(($counttime / (3600 * 24 * 30))) . '个月前'; } else if ($counttime >= 3600 * 24 * 365) { return intval(($counttime / (3600 * 24 * 365))) . '年前'; } } ```