How to calculate relative time PHP

Do you remember the post I wrote about How to calculate relative time like facebook?

The code was a little bit messy so today i'll show you a short snippet.

In my example I have stored the time in a datetime field in my MySQL database.

Then I'd like to know how many days ago was it.

// $my_database_field coud be -> "2011-06-04 09:52:01" $days_ago = (time() - $my_database_field)/86400;
Code language: PHP (php)

Easy, right? 😉

About Ricard Torres

Senior Front-end Software Engineer at Netcentric, from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.

@ricard_dev @ricard_dev

📝 Blog 🎙 Podcast

Leave a Reply

Add <code> Some Code </code> if you need to.