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

后端 未结 3 1269
余生分开走
余生分开走 2021-01-16 03:01

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:04

    PHP doesn't have a built-in function to do it for you, so you'd have to do it by hand. This function will get the difference in days, hours, minutes, or seconds and return in the format 'X days/hours/minutes/seconds':

    = 1) {
          $how_log_ago = $days . ' day' . ($days != 1 ? 's' : '');
        } else if ($hours >= 1) {
          $how_log_ago = $hours . ' hour' . ($hours != 1 ? 's' : '');
        } else if ($minutes >= 1) {
          $how_log_ago = $minutes . ' minute' . ($minutes != 1 ? 's' : '');
        } else {
          $how_log_ago = $seconds . ' second' . ($seconds != 1 ? 's' : '');
        }
        return $how_log_ago;
    ?>
    

提交回复
热议问题