Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.