How to display “12 minutes ago” etc in a PHP webpage?

Question :

How to display “12 minutes ago” etc in a PHP webpage?,

Answer :

Can anyone tell me how I can display a status message like “12 seconds ago” or “5 minutes ago” etc in a web page?

,

Here is the php code for the same:

function time_since($since) {      $chunks = array(          array(60 * 60 * 24 * 365 , 'year'),          array(60 * 60 * 24 * 30 , 'month'),          array(60 * 60 * 24 * 7, 'week'),          array(60 * 60 * 24 , 'day'),          array(60 * 60 , 'hour'),          array(60 , 'minute'),          array(1 , 'second')      );        for ($i = 0, $j = count($chunks); $i < $j; $i++) {          $seconds = $chunks[$i][0];          $name = $chunks[$i][1];          if (($count = floor($since / $seconds)) != 0) {              break;          }      }        $print = ($count == 1) ? '1 '.$name : "$count {$name}s";      return $print;  }  

The function takes the number of seconds as input and outputs text such as:

  • 10 seconds
  • 1 minute
Read More  Etiquette for selecting answers

etc

That's the answer How to display “12 minutes ago” etc in a PHP webpage?, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you