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

前端之家收集整理的这篇文章主要介绍了php 返回相对时间,如:20分钟前,3天前前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:

/**
 * 友好时间显示
 *
 * @param 
 * @arrange (512.笔记) jb51.cc
 **/
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相关文章