首页 >后端开发 >php教程 >php返回相对时间,如:20分钟前,3天前

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

WBOY
WBOY原创
2016-07-25 08:43:09986浏览
  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


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn