How to format date from timestamp in PHP?

Use strtotime function:

$timeStamp = $row['date'];
$timeStamp = date( "m/d/Y", strtotime($timeStamp));

For the date from your example 2010-06-19 18:40:51, it would return:

06/19/2010


The timestamp field updates automatically but you can disable that behavior by changing it to DEFAULT CURRENT_TIMESTAMP otherwise it will auto update. From the manual:

In a CREATE TABLE statement, the first TIMESTAMP column can be declared in any of the following ways:

With both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses, the column has the current timestamp for its default value, and is automatically updated.

With neither DEFAULT nor ON UPDATE clauses, it is the same as DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.

With a DEFAULT CURRENT_TIMESTAMP clause and no ON UPDATE clause, the column has the current timestamp for its default value but is not automatically updated.


strtotime($timeStamp) (string to time) is what you want to convert into a unix timestamp.

Then you can use the date on that, so something like date( "m/d/Y", strtotime($timeStamp) )

http://php.net/manual/en/function.strtotime.php

Tags:

Mysql

Php