Jump to content

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


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]

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.