Jump to content

[SOLVED] mysql timestamp column displays as 01/01/1970 - why?


Edward

Recommended Posts

Hi,

 

I have a MySQL table which includes a TIMESTAMP column to record the time and date a comment was posted on a blog.

 

When I display the TIMESTAMP on the site, I am formatting it in the same way I would with date(), but it's not using the database value, it's using 1st Jan 1970. I know this date has some significance, but I'm not sure why it's showing up here.

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$date = $row['date'];
echo '<p>Date: '.date(l.', '.jS.' '.F.' '.Y, $date).'</p>';
}

 

Does anyone know what I've done wrong? Thank you.

 

The second parameter to the date() function needs to be a UNIX timestamp -- an integer, you can get this by using the strtotime() function:

<?php
while($row = mysql_fetch_assoc($result)) {
     $date = $row['date'];
     echo '<p>Date: ' . date('l,jS F Y', strtotime($date)).'</p>';
}
?>

 

Ken

A TIMESTAMP column is in the format of a DATETIME data type yyyy-mm-dd hh:mm:ss. This is not the same as a Unix TIMESTAMP. To output this in any format you want use the mysql DATE_FORMAT() function in your query.

Thank you all!

 

I've used this which works:

$date = strtotime($row['date']);[code]

Thanks Ken. PFMaBiSmAd, I trust you have no objections to this method, i.e. using TIMESTAMP in my table and converting it with strtotime() before displaying it on my site? I'm not sure if you're saying I should use DATE_FORMAT over TIMESTAMP?

Thanks again!

[/code]

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.