欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php返回相对时间,如:20分钟前,3天前

程序员文章站 2022-06-07 17:13:53
...
  1. function plural($num) {
  2. if ($num != 1)
  3. return "s";
  4. }
  5. function getRelativeTime($date) {
  6. $diff = time() - strtotime($date);
  7. if ($diff return $diff . " second" . plural($diff) . " ago";
  8. $diff = round($diff/60);
  9. if ($diff return $diff . " minute" . plural($diff) . " ago";
  10. $diff = round($diff/60);
  11. if ($diff return $diff . " hour" . plural($diff) . " ago";
  12. $diff = round($diff/24);
  13. if ($diff return $diff . " day" . plural($diff) . " ago";
  14. $diff = round($diff/7);
  15. if ($diff return $diff . " week" . plural($diff) . " ago";
  16. return "on " . date("F j, Y", strtotime($date));
  17. }
复制代码

php