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.
Update: Try to use mysqli instead of mysql, this is old tutorial therefore i used mysql in it.
If you have any issue with this tutorial so let me know in the below comments section.
No comments:
Post a Comment