php function to convert Unix timestamp into minutes or hours or days like digg?

后端 未结 3 687
礼貌的吻别
礼貌的吻别 2021-01-16 02:37

i want a php function to convert a unix time stamp to like this:

15 seconds

or

45 minutes

or

3 hours

and not like this : 2sec

3条回答
  •  無奈伤痛
    2021-01-16 03:28

    If this is for a website, you might consider Timeago. Otherwise the algorithm is pretty straightforward. Something like:

    $diff = $date - $now;
    if ($diff > 2 * ONE_YEAR)
        return sprintf("%d years", round($diff / ONE_YEAR));
    else if ($diff > ONE_YEAR)
        return "1 year";
    else if ($diff > 2 * ONE_MONTH)
        return sprintf("%d months", round($diff / ONE_MONTH));
    ...etc...
    

提交回复
热议问题