Jump to content

Datetime To Seconds, Minutes, Hours, Days Differences


JustinK101

Recommended Posts

I have a `datetime` column `date_updated` that comes from mysql. I would like to format the datetime to subtract from now() to do the following logic:

 

You updated your profile X seconds ago. (if difference  0 - 59 seconds)

You updated your profile X minutes ago. (if difference 60 - 3599 seconds)

You updated your profile X hours ago.  (if difference 3600 -  86399 seconds)

You updated your profile X days ago.  (if difference >= 86400)

 

Thanks for the help guys.

Just use strtotime() on the field in question, subtract your result from the current time, then run it through a switch or some ifs to find what to print.

 

<?php
$temp = strtotime($row['date']);
$temp_diff = time() - $temp;

if($temp_diff <= 59) {
echo "You updated your profile $temp_diff seconds ago";
}
// And so on
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.