Hi friends Can anybody advise on how i can best use php and mysql to give a database record an "age" in days? I am recording record creation via timestamp at the minute Thanks
just use the mysql date and time functions to work out the age between now and the time inserted, then output that google mysql date and time functions
http://codepad.org/bCYtCYmX <?php // Calculate the days since a MySQL timestamp. // Roy Laurie Software < http://www.roylaurie.com > $datestamp = '2000-02-04 01:34:00'; // get this from mysql $unixstamp = strtotime($datestamp); $secondsDiff = time() - $unixstamp; $days = $secondsDiff / 60 / 60 / 24; $days = floor($days); echo "It has been {$days} days since {$datestamp}.\n"; PHP:
Here are the two best ways to do this. MySQL Perspective: INSERT INTO my_table SET date_time = NOW() Code (markup): That will give date_time something that looks like '2010-02-11 23:59:59' PHP Perspective: $sql = "INSERT INTO my_table SET date_time = '".date('Y-m-d H:i:s')."'"; mysql_query($sql); Code (markup): I prefer using the mysql function NOW(). Using the mysql timestamp option is bad because everytime that record is updated it will update that time. If you want that the I suggest having a created_date_time column and a updated_date_time column.