<?php
function dateWord($from, $now) {
$timezone = new DateTimeZone('Asia/Shanghai');
$now = new DateTime($now, $timezone);
$from = new DateTime($from, $timezone);
$between = $now->diff($from);
if(!$between->invert) return false;
/** 如果超过了一年 **/
if($between->y)
return $from->format('Y年m月d日');
/** 一年内大于七天 **/
if($between->days > 6)
return $from->format('n月j日');
/** 一个礼拜内但是大于两天**/
if($between->days > 1)
return $between->format('%d天前');
/** 如果是昨天 **/
if($between->days)
return $from->format('昨天 H:i');
/** 如果一天之内超过一个小时 **/
if($between->h > 1)
return $between->format('%h小时前');
if($between->i > 1)
return $between->format('%i分钟前');
return $between->s ? $between->format('%s秒前') : '刚刚';
}
function dateWordToHtml($from, $now = 'now') {
$dateWord = dateWord($from, $now);
$from = new DateTime($from, new DateTimeZone('Asia/Shanghai'));
$fromWord = $from->format('Y年m月d日 H:i');
return "<span title=\"$fromWord\">$dateWord</span>";
}
//dateWord()函数对应的是Typecho原版的返回,dateWordToHtml()函数则是对应你修改的那个函数
echo dateWordToHtml("2014/4/14 9:32");
- Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.7
- <span title="2014年04月14日 09:32">6天前</span>
- Output for 5.2.0 - 5.2.17
- Fatal error: Call to undefined method DateTime::diff() in /in/WZ96n on line 6
Process exited with code 255. - Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6
- Fatal error: Class 'DateTimeZone' not found in /in/WZ96n on line 3
Process exited with code 255. - Output for 4.3.2 - 4.3.11, 4.4.0 - 4.4.9
- Fatal error: Cannot instantiate non-existent class: datetimezone in /in/WZ96n on line 3
Process exited with code 255. - Output for 4.3.0 - 4.3.1
- Fatal error: Cannot instantiate non-existent class: datetimezone in /in/WZ96n on line 3
preferences:
208.3 ms | 401 KiB | 370 Q