php返回相对时间,如:20分钟前,3天前
时间:2021-07-01 10:21:17
帮助过:26人阅读
- function plural($num) {
- if ($num != 1)
- return "s";
- }
-
- function getRelativeTime($date) {
- $diff = time() - strtotime($date);
- if ($diff<60)
- return $diff . " second" . plural($diff) . " ago";
- $diff = round($diff/60);
- if ($diff<60)
- return $diff . " minute" . plural($diff) . " ago";
- $diff = round($diff/60);
- if ($diff<24)
- return $diff . " hour" . plural($diff) . " ago";
- $diff = round($diff/24);
- if ($diff<7)
- return $diff . " day" . plural($diff) . " ago";
- $diff = round($diff/7);
- if ($diff<4)
- return $diff . " week" . plural($diff) . " ago";
- return "on " . date("F j, Y", strtotime($date));
- }
|
php