0

Is it possible to load a timestamp from mysql database, subtract it with current timestamp(if needed) and convert it into something like a minute ago, 2 minutes ago, a hour ago, etc ? I need help to display the timestamp with that format.

<?php
function getuser_get($dt)
{
 $query="SELECT huffid,fullname, photourl, gender, country, city, lastupdate, dob,
  concat('[',(select GROUP_CONCAT('{".'"sportname.":"'."',sportname,'".'"}'."' SEPARATOR ',') 
  from personal_sport inner join sports on 
  personal_sport.sportid=sports.id where personal_sport.huffid=personal.huffid),']')
  as sports FROM personal 
  order by fullname asc
  limit 20;";
 
 return $query;
}
?>

in mysql database, the lastupdate column format is like this 2018-06-21 01:05:07, and i need to convert it into "time ago" dynamically each time the app is being refreshed. Can anyone help, please? Thank you in advance.

Wira Xie
  • 819
  • 5
  • 24
  • 46
  • 2
    yes quite possible, here's the PHP way https://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago – Kevin Jun 21 '18 at 08:54

1 Answers1

0

As it is tagged PHP, here is a quick way to do using DateTime::diff:

$date = new DateTime('2017-06-21 01:05:07');
$diff = $date->diff((new DateTime()));
echo $diff->format('%Y years %m months %d days %H hours %i minutes %s seconds ago');

While testing:

01 years 0 months 0 days 09 hours 53 minutes 46 seconds ago

To only display the relevant informations please refer to this answer (which I wouldn't copy-paste)

AymDev
  • 6,626
  • 4
  • 29
  • 52
  • It has limitations on versions PHP 5 >= 5.3.0 < 5.5.0, programmer need to check the PHP version before using this code! – Mangesh Sathe Jun 21 '18 at 09:58
  • @MangeshSathe the documentation may need to be updated on this method, I run it with PHP 7.2 and never had any issue with it before.. As long as no PHP version constraint is mentionned by OP I won't change my answer. – AymDev Jun 21 '18 at 10:09