chriskiely Posted May 14, 2007 Share Posted May 14, 2007 Hi, As a final touch to my current project I am trying to display a "Last Updated: " field on the user profile pages of the website. The SQL database is updating the field last_updated automatically any time a record is altered, and this is working fine... Structure details from phpMyAdmin (in table 'users'): last_updated timestamp ON UPDATE CURRENT_TIMESTAMP No 0000-00-00 00:00:00 However when I try to output this date I get the result: Profile Updated: January 1, 1970 The code I am using is as follows... require_once('../../mysql_connect.php'); $last_updated_query = "SELECT last_updated FROM users WHERE (user_name='$user_name')"; $last_updated = @mysql_query($last_updated_query); while ($row = mysql_fetch_array ($last_updated)) { $updated=$row['last_updated']; $format='F j, Y'; $output_date = date ($format, $updated); echo 'Profile Updated:'.$output_date; } The values for last_updated in the database are not in 1970! Can anyone tell me what I'm doing wrong here? Thanks, Chris Quote Link to comment https://forums.phpfreaks.com/topic/51313-solved-problems-displaying-date/ Share on other sites More sharing options...
kenrbnsn Posted May 14, 2007 Share Posted May 14, 2007 The second parameter to the date() function is an UNIX timestamp, an integer. You are passing it a text string, so it defaults to the PHP zero date of January 1, 1970. You need to convert the ASCII string to a UNIX timestamp with the strtotime() function: <?php while ($row = mysql_fetch_array ($last_updated)) { $updated=$row['last_updated']; $format='F j, Y'; $output_date = date ($format, strtotime($updated)); echo 'Profile Updated:'.$output_date; } ?> BTW, the above code can be shortened: <?php while ($row = mysql_fetch_array ($last_updated)) echo 'Profile Updated:' . date('F j, Y', strtotime($row['last_updated'])); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/51313-solved-problems-displaying-date/#findComment-252714 Share on other sites More sharing options...
chriskiely Posted May 14, 2007 Author Share Posted May 14, 2007 Thanks Ken that's done it! ..and I did originally code it the more elegant way, but in my frustration of trying to get the code to work I tried seperating everything out. Thanks again, topic solved! Chris Quote Link to comment https://forums.phpfreaks.com/topic/51313-solved-problems-displaying-date/#findComment-252718 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.