Alphatester77
Alphatester77

Reputation: 702

Display mysql datetime in human readable format with php?

Am saving to a mysql table using:

$query = mysql_query("INSERT INTO test SET postit='$postit',postdate=NOW()");

I'm then trying to display it using:

echo "<li>" . date("D, d M y H:i:s O",$row['timestamp']) . " - " . $row['postit'] . "</li>";

It's saving the correct time in the database, however it renders:

Thu, 01 Jan 70 01:00:00 +0100

Anyone point out the stupidity?

Upvotes: 3

Views: 7757

Answers (2)

PeeHaa
PeeHaa

Reputation: 72672

I always like to use this function for that:

function parse_sql_timestamp($timestamp, $format = 'd-m-Y')
{
    $date = new DateTime($timestamp);
    return $date->format($format);
}

This way we can even go beyond 2038 ;)

Upvotes: 2

Tim Withers
Tim Withers

Reputation: 12059

The PHP date() function uses a Unix timestamp as the second variable in the function. What you are passing to the function is a MySQL time stamp. Try using:

echo date("D, d M y H:i:s O",strtotime($row['timestamp']));

Upvotes: 10

Related Questions