PHP Date Format vs MySQL Date Format

Today I had the pleasure of discovering (through an hour of annoying debugging) that PHP’s formatting of dates is not exactly identical to MySQL’s date formatting. I was using a PHP constant to define a date format that I used all across my website, including in my SQL queries. Since I discovered that the formatting placeholders differed, I needed to create a separate constant to be used in MySQL’s DATE_FORMAT.

// Use two different constants for the global date format, one for PHP and one for SQL.
define("DATE_FORMAT_PHP", "F j, Y"); // August 18, 2012
define("DATE_FORMAT_SQL", "%M %e, %Y"); // August 18, 2012

PHP Date Format
MySQL Date Format

Leave a Reply

Your email address will not be published. Required fields are marked *