3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
<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.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.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