PHP将DateTime对象转化为友好时间显示的实现代码

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

/**
* 友好日期时间
*
* @param DateTime $datetime 日期时间
* @param int $size 精确到位数
* @throws \InvalidArgumentException
* @return string
*/
function friendly_date($datetime, $size=1)
{
if (is_int($datetime)) {
$datetime = new \DateTime($datetime);
}
if (!($datetime instanceof \DateTime)) {
throw new \InvalidArgumentException('invalid "DateTime" object');
}
$now = new \DateTime();
$interval = $now->diff($datetime);
$intervalData = array(
$interval->y, $interval->m, $interval->d,
$interval->h, $interval->i, $interval->s,
);
$intervalFormat = array('年', '个月', '天', '小时', '分种', '秒');
foreach($intervalData as $index=>$value) {
if ($value) {
$intervalData[$index] = $value . $intervalFormat[$index];
} else {
unset($intervalData[$index]);
unset($intervalFormat[$index]);
}
}
return implode('', array_slice($intervalData, 0, $size));
}

相关文章

十大使用PHP框架的理由

十大使用PHP框架的理由

PHP框架提供了一个用以构建web应用的基本框架,从而简化了用PHP编写web应用程序的流程。换言之,PHP框架有助于促进快速应用开发( RAD ),不但节省开发时间、有助于建立更稳定的...

php删除指定目录的方法

本文实例讲述了php删除指定目录的方法。分享给大家供大家参考。具体分析如下: 这段代码可实现递归删除子目录的功能 <?php /** * Delete a file,...

PHP中session使用方法详解第1/2页

由于 Session 是以文本文件形式存储在服务器端的,所以不怕客户端修改 Session 内容。实际上在服务器端的 Session 文件,PHP 自动修改 session 文件的权限,...

apache rewrite_module模块使用教程

把 [url]http://wwww.aaaaaaaaa.com/bbb.php?id=888[/url] 的地址形式改为 [url]http://wwww.aaaaaaaaa.com/...

php mail to 配置详解

复制代码 代码如下: [mail function] ; For Win32 only. SMTP = mail3.focuschina.com smtp_port = 25 ; For...