Jump to content

Convert date from a database query


hazniet

Recommended Posts

I have a simple database for events. I have a column named "date" that I would like to display as "March 8, 2007" on the webpage. What do I need to add to change the date format? Here is my php code:

 

$connection = mysql_connect($server, $user, $pass);

mysql_select_db($db) or die ("Unable to select database!");

 

$query = "SELECT * FROM events ORDER by date";

$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

 

if (mysql_num_rows($result) > 0) {

    echo "<h2>2006-2007</h2><table width='100%'>";

    while($row = mysql_fetch_assoc($result)) {

        echo "<tr>";

        echo "<td width='33%'>".$row['date']."</td>";

        echo "<td width='25%'>".$row['event']."</a></td>";

echo "<td>".$row['location']."</td>";

        echo "</tr>";

    }

    echo "</table><hr>";

}

else {

    echo "No rows found!";

}

 

mysql_free_result($result);

mysql_close($connection);

 

?>

Link to comment
https://forums.phpfreaks.com/topic/46215-convert-date-from-a-database-query/
Share on other sites

You can use strtotime() and date() to achieve that.

 

Both those functions are listed here: http://sg.php.net/manual/en/ref.datetime.php

 

strtotime() will convert the mysql date into a timestamp, and date() can format the timestamp according to your specification.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.