How to convert MySQL TIMESTAMP to date time in PHP
To do this the OO (and most flexible) way use DateTime
class and use the static createFromFormat
method to instantiate a new DateTime
object:
$new_datetime = DateTime::createFromFormat ( "Y-m-d H:i:s", $row["timestamp"] );
Now you can use the $new_datetime
object to generate any string representation you'd like by calling the object's format
method:
echo $new_datetime->format('d/m/y, H:i:s');
To boot, you since you've a DateTime
object you can now also to any manner of transformation (like shifting timezones or adding days), comparison (greater or less than another DateTime
), and various time calculations (how many days/months/etc... between this and another DateTime
).
DateTime:http://php.net/manual/en/class.datetime.php Learn it. Love it. Live it.
Normally in MySQL date timestamp save time as YYYY-MM-DD HH:MM:SS (2016-12-20 18:36:14)
formate you can easily convert them as your wish using date formate but have to convert your input to time first. Following will do the trick
$formatted_datetime = date("d/m/y, H:i:s", strtotime($row["timestamp"]));
Why not make MySQL do the work? And do you really want mm/dd/yy, not dd/mm/yy?
SELECT *, DATE_FORMAT(timestamp, '%m/%d/%y, %T') formatted_date FROM ....
Then you can extract the formatted date as $row['formatted_date'].
See https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format