Sun 22 Apr 2007
Date format in MySQL and PHP
Posted by NZEYIMANA Emery Fabrice under English , MySQL , PHP , SoftwareComments Off
For formatting dates in MySQL one can use the DATE_FORMAT function like DATE_FORMAT(`date_col or value`, dateFormatString)
In a select query, that looks like SELECT DATE_FORMAT(`date_col or value`, dateFormatString) FROM `tableName`
. In case one wants to use the server current date, the query would look like SELECT DATE_FORMAT(NOW(), dateFormatString) FROM `tableName`
For a detailed explanation of the options that can be in a dateFormatString, read the MySQL date related page (dev.mysql.com/doc/).
In the general examples below, I will use the date 22 April 2007 16:15:23 (date of creation of this article)
Language | Format String | Output |
---|---|---|
English | %m/%d/%Y | 04/22/2007 |
English | %m/%d/%Y %H:%i | 04/22/2007 16:15 |
English | %a, %D %b %Y %H:%i | Sun, 22nd Apr 2007 16:15 |
Français | %d-%m-%Y | 22-04-2007 |
Français | %d-%m-%Y %H:%i | 22-04-2007 16:15 |
Français | %a, %D %b %Y %H:%i | Dim, 22 Avr 2007 16:15 |
Ikinyarwanda | %d-%m-%Y | 22-04-2007 |
Ikinyarwanda | %d-%m-%Y %H:%i | 22-04-2007 16:15 |
Ikinyarwanda | %a, %D %b %Y %H:%i | Cyu, 22 Mata 2007 16:15 |
Svenska | %Y-%m-%d | 2007-04-22 |
Svenska | %Y-%m-%d %H:%i | 2007-04-22 16:15 |
Svenska | %Y-%m-%d %H:%i | 22-april-2007 16:15 |
For PHP formatString, visit http://www.php.net/date