Question

My host makes working with MySQL exceedingly difficult; to avoid having to work with MySQL, is there any way to add timestamps to articles by using a table-less "time ago" function, then using echo to display the difference between the current date and the "time ago" date?

Was it helpful?

Solution

Add a DATETIME column to your article table. Then when you insert/update an article you can set the time with

$row = array(
    'title' => 'My Article',
    ...
    'date' => date("Y-m-d H:i:s")
);

$db->insert('article', $row);

When you pull the article out you can get the difference:

$row = $db->get('article', 32);

$diff = time() - strtotime($row['date']); 

print $diff . ' in seconds';

You can then do something with the seconds $diff like divide by minutes, hours, days, etc.. to show the type of time ago you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top