简体   繁体   中英

PHP, load timestamp from mysql and convert into time ago string

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM