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

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
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));
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:python获取远程设备的IP地址

下一篇:php不破坏单词截取子字符串