{"id":1752,"date":"2009-02-20T02:52:04","date_gmt":"2009-02-20T06:52:04","guid":{"rendered":"http:\/\/teamtutorials.com\/?p=1752"},"modified":"2013-12-07T19:33:59","modified_gmt":"2013-12-08T00:33:59","slug":"convert-a-mysql-date-field-using-php-functions","status":"publish","type":"post","link":"https:\/\/teamtutorials.com\/web-development-tutorials\/convert-a-mysql-date-field-using-php-functions","title":{"rendered":"Convert a MySQL date field using PHP Functions"},"content":{"rendered":"
Warning:<\/strong> This tutorial uses old techniques. It is insecure and will leave your server vulnerable SQL Injection attacks<\/strong>.This tutorials also uses mysql_ functions that are no longer support. For updated tutorials look for a PDO<\/a> or MySQLi tutorial.This post will be delete or revised in the future.<\/p>\n<\/div>\n Today I am going to show you how to convert data returned from a MySQL database that was store as a date data type into a user friendly format. To do this you can take two different approaches. One way to do it is to use the function date_format() function in your query. The other way to to modify the date after it is returned.<\/p>\n Since you are trying to convert the date I am assuming you can connect to a database, runa query, and display the results. If not check out some of our older tutorials:<\/p>\n You can use the function in the query to convert the date or after the results are returned.<\/p>\n Using date_format() <\/p>\n Here is an example of how to call the function in a query:<\/p>\n Then when your results are returned simple echo $row[newdate]. The date I used will output the date in this format: Feb 18th (abbreviated month and numeric day with suffix). You can pretty much do this the same way after your results are returned. strtotime will convert a string date to a time stamp.<\/p>\n Other formatting option for date_format()<\/p>\n edit: \t\t$day = date(“d”,strtotime($row[‘date’])); would give you Mar 03<\/p>\n","protected":false},"excerpt":{"rendered":" Today I am going to show you how to convert data returned from a MySQL database that was store as a date data type into a user friendly format. To do this you can take two different approaches. One way to do it is to use the function date_format() function in your query. The other way to to modify the date after it is returned.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[73,24,17],"tags":[],"yoast_head":"\nHow to Access a MySQL Database Using PHP<\/a><\/h2>\n
Creating a Form that will Search a MySQL Database<\/a><\/h2>\n
\r\n$query = mysql_query(\"select * date_format(date, '%b %d') as newdate from `table`\")\r\n<\/pre>\n
\nYou need to pass the date string and the format mask to the function: date_format($date, $format)<\/p>\n
\nFor example:<\/p>\n\r\n\t$query = mysql_query(\"select * from `table` \");\t\r\n\twhile ($row = mysql_fetch_array($query)){\r\n\t\t$newdate = date_format(strtotime($row[date]), '%b %d');\r\n\t}\r\n<\/pre>\n
\n
\n Specifier<\/th>\n Description<\/th>\n<\/tr>\n \n %a<\/td>\n Abbreviated weekday name (Sun..Sat)<\/td>\n<\/tr>\n \n %b<\/td>\n Abbreviated month name (Jan..Dec)<\/td>\n<\/tr>\n \n %c<\/td>\n Month, numeric (0..12)<\/td>\n<\/tr>\n \n %D<\/td>\n Day of the month with English suffix (0th, 1st, 2nd, 3rd, \u2026)<\/td>\n<\/tr>\n \n %d<\/td>\n Day of the month, numeric (00..31)<\/td>\n<\/tr>\n \n %e<\/td>\n Day of the month, numeric (0..31)<\/td>\n<\/tr>\n \n %f<\/td>\n Microseconds (000000..999999)<\/td>\n<\/tr>\n \n %H<\/td>\n Hour (00..23)<\/td>\n<\/tr>\n \n %h<\/td>\n Hour (01..12)<\/td>\n<\/tr>\n \n %I<\/td>\n Hour (01..12)<\/td>\n<\/tr>\n \n %i<\/td>\n Minutes, numeric (00..59)<\/td>\n<\/tr>\n \n %j<\/td>\n Day of year (001..366)<\/td>\n<\/tr>\n \n %k<\/td>\n Hour (0..23)<\/td>\n<\/tr>\n \n %l<\/td>\n Hour (1..12)<\/td>\n<\/tr>\n \n %M<\/td>\n Month name (January..December)<\/td>\n<\/tr>\n \n %m<\/td>\n Month, numeric (00..12)<\/td>\n<\/tr>\n \n %p<\/td>\n AM or PM<\/td>\n<\/tr>\n \n %r<\/td>\n Time, 12-hour (hh:mm:ss followed by AM or PM)<\/td>\n<\/tr>\n \n %S<\/td>\n Seconds (00..59)<\/td>\n<\/tr>\n<\/table>\n
\nThis can also be done using the date function. Something like:<\/p>\n
\n\t\t$month = date(“M”,strtotime($row[‘date’]));<\/p>\n