Jump to content

[SOLVED] Problems displaying date...


chriskiely

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/51313-solved-problems-displaying-date/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.