PHP Time Ago Function

PHP Time Ago Function

Demo Download

Time Ago Function is a function that is commonly used to display time in human understandable format, as you have seen many times on social networks, whenever you post any status on your Facebook, Twitter or any other network, it says posted few seconds ago, 1 min ago, 1 hour age, etc. This is actually done by subtracting the current time from the post published time.

For this purpose we need to store a date in database, date should be in MySQL standard date and time format (“Y-m-d H:i:s”).

Lets create a database with name register.

Create a table with name time_table with column id and trn_date.

After creating the above table, you need to insert a record, or i have attached sql file if you can import that sql file so it is better because it will already have data in table.

Now create php file with the following code:

Note: In the above code first i created a function to calculate posted time and then i used mysql_connect() function, you will need to provide your database username and password, then i simply echo time ago function with parameters.

If you have any issue with this tutorial so let me know in the below comments section.

Article By
Javed Ur Rehman is a passionate blogger and web developer, he loves to share web development tutorials and blogging tips. He usually writes about HTML, CSS, JavaScript, Jquery, Ajax, PHP and MySQL.

4 Comments

  1. Javed Ur Rehman, all ur tutorials have been 100% working and helpful, easily modified better understood. i like you man. keep up the good work.

  2. Hey can you please do a time ago function in mysqli please. I love your tutorial and I would love your work more.

Leave a Reply

Your email address will not be published. Required fields are marked *