最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
datetime类型日期时间转换成中文表示
时间:2022-06-24 21:42:47 编辑:袖梨 来源:一聚教程网
下面是一个将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));
}
相关文章
- 知聊如何设置收费 03-26
- 小宇宙app如何上传音频 03-26
- 如何使用作业帮家长版 03-26
- 民生银航app如何修改预留手机号 03-26
- 俄罗斯引擎yandex入口怎么无需登录 03-26
- 如何在PPT中插入背景音乐并设置播放 03-26