当前位置:网站首页>php 将时间戳转化为 刚刚、几分钟前、几小时前、几天前 格式
php 将时间戳转化为 刚刚、几分钟前、几小时前、几天前 格式
2022-08-08 23:01:00 【还相信光吗?】
下面是php实现的几种方式 可以自己选用
//把时间戳转换为几分钟或几小时前或几天前
function wordTime($time) {
$time = (int) substr($time, 0, 10);
$int = time() - $time;
$str = '';
if ($int <= 30){
$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;
}function mdate($time = NULL) {
$text = '';
$time = $time === NULL || $time > time() ? time() : intval($time);
$t = time() - $time; //时间差 (秒)
$y = date('Y', $time)-date('Y', time());//是否跨年
switch($t){
case $t == 0:
$text = '刚刚';
break;
case $t < 60:
$text = $t . '秒前'; // 一分钟内
break;
case $t < 60 * 60:
$text = floor($t / 60) . '分钟前'; //一小时内
break;
case $t < 60 * 60 * 24:
$text = floor($t / (60 * 60)) . '小时前'; // 一天内
break;
case $t < 60 * 60 * 24 * 3:
$text = floor($time/(60*60*24)) ==1 ?'昨天 ' . date('H:i', $time) : '前天 ' . date('H:i', $time) ; //昨天和前天
break;
case $t < 60 * 60 * 24 * 30:
$text = date('m月d日 H:i', $time); //一个月内
break;
case $t < 60 * 60 * 24 * 365&&$y==0:
$text = date('m月d日', $time); //一年内
break;
default:
$text = date('Y年m月d日', $time); //一年以前
break;
}
return $text;
}
希望对于大家有所帮助!!!!
边栏推荐
猜你喜欢

第二课:概率论

【YOLOv5】6.0环境搭建(不定时更新)

wps表格分两页断开怎么办?wps表格分两页断开的解决方法

MES docks with Simba to realize IMEI number writing and coupling test of Spreadtrum platform

Analysis of WLAN - Wireless Local Area Network

Kubernetes 资源编排系列之二: Helm 篇

laravel6框架跨域请求利器之 Laravel CORS 扩展包的安装和使用

Virtualization type (with picture)

【PP-YOLOv2】训练自定义的数据集

让IPv6强大的关键——NDP邻居发现协议
随机推荐
C language library function summary2019.10.31
JSDay2-多个数组的交集
深拷贝与浅拷贝
4399 it operations intern interview experience
用工具实现 Mock API 的整个流程
wsgw login packet capture record
IMConversation 或 IMUser 类型数据
新安装Laravel Framework 6.18.35 php artisan migrate 报错
wps表格分两页断开怎么办?wps表格分两页断开的解决方法
iptables防火墙内容全解
MES对接Simba实现展讯平台 IMEI 写号与耦合测试
WeChat small program "decompiled" combat "behind to unpack the eggs
DHCP's defense mechanism - DHCP Snooping (DHCP snooping)
MES docks with Simba to realize IMEI number writing and coupling test of Spreadtrum platform
ArcPy set the unique identification code
蚂蚁森林 离线爬虫自动收能量,养小鸡,等各种操作
基于.NET6、FreeSql、若依UI、LayUI、Bootstrap构建插件式的CMS
Excuse me: is it safe to pay treasure to buy fund on
三国战绩物品序号.txt
wps a列不见了怎么办?wps a列不见了的解决方法