![7a6048d8cd08a83d04d16a6cd9fb9a1f.png](https://i-blog.csdnimg.cn/blog_migrate/f9da298089e9dead096ff518a7d24fb3.jpeg)
php中文网最新课程
每日17点准时技术干货分享
![e004b24621163e1cf87631471cc2489d.png](https://i-blog.csdnimg.cn/blog_migrate/7ce71c6e6550dd49396421956c2b2906.jpeg)
php时间戳这个概念想必对刚入门的小伙伴来说,都是有一定的了解。咱们第一时间都会想到两非常个重要的时间函数Date / Time 。首先给大家简单介绍下这两个函数的相关知识。
Date/Time 函数是允许您从 PHP 脚本运行的服务器上获取日期和时间。您可以使用 这两个 函数通过不同的方式来格式化日期和时间。这里要注意,它们是依赖于服务器的本地设置的,而且使用这些函数时请记住要考虑夏令时和闰年。
那么本篇文章就是给大家详细介绍关于php时间分钟转换(小时、天...)的具体用法及如何转换成几分钟前等的时间格式。(小时、天...)
一、php时间转换为分钟(天、小时)的具体代码示例如下:
function
format_date(
$time
){
if
(!
is_numeric
(
$time
)){
$time
=
strtotime
(
$time
);
}
$t
=time()-
$time
;
$f
=
array
(
'31536000'
=>
'年'
,
'2592000'
=>
'个月'
,
'604800'
=>
'星期'
,
'86400'
=>
'天'
,
'3600'
=>
'小时'
,
'60'
=>
'分钟'
,
'1'
=>
'秒'
);
foreach
(
$f
as
$k
=>
$v
) {
if
(0 !=
$c
=
floor
(
$t
/(int)
$k
)) {
return
''
.
$c
.
' '
.
$v
.
'前'
;
}
}
}
二、php 时间戳转换为多少分钟(天、小时)前的具体代码示例如下:
function
get_last_time(
$time
)
{
$todayLast
=
strtotime
(
date
(
'Y-m-d 23:59:59'
));
$agoTimeTrue
= time() -
$time
;
$agoTime
=
$todayLast
-
$time
;
$agoDay
=
floor
(
$agoTime
/ 86400);
if
(
$agoTimeTrue
< 60) {
$result
=
'刚刚'
;
}
elseif
(
$agoTimeTrue
< 3600) {
$result
= (
ceil
(
$agoTimeTrue
/ 60)) .
'分钟前'
;
}
elseif
(
$agoTimeTrue
< 3600 * 12) {
$result
= (
ceil
(
$agoTimeTrue
/ 3600)) .
'小时前'
;
}
elseif
(
$agoDay
== 1) {
$result
=
'昨天 '
;
}
elseif
(
$agoDay
== 2) {
$result
=
'前天 '
;
}
else
{
$format
=
date
(
'Y'
) !=
date
(
'Y'
,
$time
) ?
"Y-m-d"
:
"m-d"
;
$result
=
date
(
$format
,
$time
);
}
return
$result
;
}
注:function get_last_time(){} 获取当天最大时间
通过本篇文章关于php时间分钟转换(天、小时)等相关知识的介绍,希望对有需要的朋友有所帮助!
-END-
声明:本文选自「 php中文网 」,搜索「 phpcnnew 」即可关注!
▼