Monday, October 27, 2008

MySQL DATETIME and PHP

If you ever had trouble with the date conversions between MySQL's DATETIME field and PHP's date function, here is a way to deal with them.

If you want to take the date from MySQL and display it a certain way with the DATE function in PHP you first need to convert it.  Use the following code:

date(formatstring, strtotime($rowentry));

formatstring is your format string for your date (see date reference)
strtotime is a function in PHP that takes a string
$rowentry is the DATETIME data from your MySQL database

No comments: