Time ago PHP function

time ago php function

Like most social networks today, instead of displaying the exact date for a post they use a time ago time format. I find it very practical. That helps the user get the information with no need to deal with different date and time formats.

You can add the following 2 functions to your php class file and call the function ago when needed. While calling the function make sure the parameter you’re passing is converted to a date object.

You may use something like this:

Time ago PHP function:

Quick Tips:

If you are using a framework like CakePHP you should probably add the time ago PHP functions to your AppController file so you can access it from any other controller.

For WordPress developers, it already has that time ago function which can be used in your theme or plugin. Here is an example for showing the time difference between the time a comment was posted and the current time:

You can read more about it here

This is not the unique solution to do the time ago in PHP, let me know in the comments sections if you have a better way.