Home > php教程 > php手册 > body text

datetime类型日期时间转换成中文表示

WBOY
Release: 2016-06-13 10:03:36
Original
1729 people have browsed it

下面是一个将datetime日期时间转换成年\', \'个月\', \'天\', \'小时\', \'分种\', \'秒\'来显示,有需要的朋友可以参考一下。

下面是一个将datetime日期时间转换成年', '个月', '天', '小时', '分种', '秒'来显示,有需要的朋友可以参考一下。

/**
* 友好日期时间
*
* @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));
}
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!