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

yipeiwu_com6年前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));
}

相关文章

解析CI的AJAX分页 另类实现方法

看了一下CI的分页类没有写到关于AJAX的内容,也在论坛上看到其他几位大神写的分页类扩展,感觉其实是没有必要。在现有的基础上做了一下小小的改动还是能实现的。下面进入正题:CI的原生分页类...

php在页面中调用fckeditor编辑器的方法

刚才在论坛上看到一个童鞋分享的方法,感觉不是很全面,现在分享下我的! 复制代码 代码如下: PHP页面: /* 编辑器 */ include_once "../include/fcked...

php绝对路径与相对路径之间关系的的分析

php绝对路径与相对路径之间关系的的分析

php中好像不能像asp那样用“/”表示根目录,代之以$_SERVER['DOCUMENT_ROOT'],其它则相同:../表示向上一层。./表示当前层。假如现在a/b/c/s.php要...

PHP下一个非常全面获取图象信息的函数

复制代码 代码如下:<?php function getimageinfo(img) { //img为图象文件绝对路径 img_info = getimagesize(img);...

php使用ereg验证文件上传的方法

本文实例讲述了php使用ereg验证文件上传的方法。分享给大家供大家参考。具体分析如下: ereg格式如下: 复制代码 代码如下:ereg(正规表达式,字符串,[匹配部分数组名]); 这...