Nick
Nick

Reputation: 425

Convert timestamp to readable date/time with PHP

I have a field in an MySQL database named last_login which is stored as an int(10). I would like to echo the last_login data as a readable date/time. Here is the code I currently have:

$timestamp = 1291575177;
echo date("h:i:s",$timestamp);

What is the proper way to add the last_login data from the MySQL database into the above code where the 1291575177 is?

Upvotes: 0

Views: 2134

Answers (2)

Praveen kalal
Praveen kalal

Reputation: 2129

Use this:

echo date('Y-m-d:H:i:s',1291575177);

The output of that would be 2010-12-05:18:52:57.

Upvotes: 3

Robin Orheden
Robin Orheden

Reputation: 2764

$connection = mysql_connect("...");

$result = mysql_query("SELECT timestamp FROM example_table LIMIT 1", $connection);
$row = mysql_fetch_assoc();
echo(date('Y-m-d H:i:s', intval($row['timestamp']))); // assuming that row['timestamp'] is a valid unix timestamp

mysql_close($connection);

Upvotes: 1

Related Questions