How to format database dates in PHP


To format the DATETIME field taken from the database, you have to turn it into a timestamp.

The DATETIME that comes from your query is actually a string. So, to format it, just use strtotime PHP function:

$sql = "SELECT date FROM table WHERE id = " . $id;
$row = db->select_single($sql); // just an example
$date = $row['date'];
echo date("d.m.Y H:i:s", strtotime($date));

Many times people have problems with formating dates with PHP, but when you get the grip, it really is easy.

About the author

Zvonko Biškup

A passionate web developer with more than 13 years experience in PHP and other web related technology. My main interest is WordPress and new PHP frameworks.
Owner of Influendo Programming.
Enjoys exploring new stuff and possibilities, using new technologies and just having fun developing awesome stuff.

<span class="dsq-postid" data-dsqidentifier="56">4 comments</span>