ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ $datetime1 = new DateTime(); $datetime2 = new DateTime('2018-08-16'); $interval = $datetime1->diff($datetime2); list($y, $m, $d) = explode('-', $interval->format('%Y-%m-%d')); echo "距世界杯还有{$y}年{$m}个月{$d}天"; $now=time(); $end=strtotime('2018-8-16 00:00:00'); $d=$end-$now; $y = date('Y', $d) - 1970; $m = date('n', $d) - 1; $d = date('j', $d) - 1; printf('还有%d年%d月%d天', $y, $m, $d); ~~~ ~~~ function getDiffTime($timestamp) { $datetime = new DateTime(date('Y-m-d H:i:s', $timestamp)); $datetime_now = new DateTime(); $interval = $datetime_now->diff($datetime); list($y, $m, $d, $h, $i, $s) = explode('-', $interval->format('%y-%m-%d-%h-%i-%s')); if ((($result = $y) && ($suffix = '年前')) || (($result = $m) && ($suffix = '月前')) || (($result = $d) && ($suffix = '天前')) || (($result = $h) && ($suffix = '小时前')) || (($result = $i) && ($suffix = '分钟前')) || (($result = $s) && ($suffix = '刚刚'))) { return $suffix != '刚刚' ? $result . $suffix : $suffix; } } ~~~