tags:

views:

45

answers:

2
$sql = "SELECT * FROM news ORDER BY `news_id` DESC LIMIT 1";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$date = $row['time_posted'];
echo "<i> " .date("Y/m/d", $date) . "</i>: ";

I used timestamp in mysql. So, what's the problem?

A: 

The unix epoch (timestamp 0) is January 1, 1970 at midnight. If you live in a timezone behind GMT, it'll show up as sometime in the evening of December 31, 1969. $date is most likely 0

Michael Mrozek
... or non-numeric.
zneak
`date()` warns if you give it a non-numeric variable.
Michael Mrozek
`date()` _notices_ when you give it a non-numeric variable and assumes 0. Most setups (unfortunately) ignore notices.
zneak
Ah, that would explain it
Michael Mrozek
+3  A: 
date('Y/m/d', strtotime($date));

TIMESTAMP columns are not displayed as unix timestamps (anymore).

TIMESTAMP columns are displayed in the same format as DATETIME columns. In other words, the display width is fixed at 19 characters, and the format is 'YYYY-MM-DD HH:MM:SS'.

timdev
In other words, to expand. You've retrieved "2010-04-25 19:36:05" from MySQL and passed that directly to PHP. `date()` is expecting a numeric value, so the string's invalid and treated as NULL, which gets cast to 0, which is converted into the epoch, and adjusted for your timezone. You're somewhere WEST of Greenwich, so your local time at time '0' was in 1969 somewhere.
Marc B
@Marc - a real Rube Goldberg chain of events. Great explanation.
Michael Petrotta