简体   繁体   中英

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?

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']));

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 ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM