当前位置 : 主页 > 网络编程 > PHP >

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

来源:互联网 收集:自由互联 发布时间:2021-07-03
function plural($num) { if ($num != 1) return "s";} function getRelativeTime($date) { $diff = time() - strtotime($date); if ($diff60) return $diff . " second" . plural($diff) . " ago"; $diff = round($diff/60); if ($diff60) return $diff . "
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));
}
网友评论