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时间分钟转换(天、小时)等相关知识的介绍,希望对有需要的朋友有所帮助!
【相关文章推荐】