Page 1 of 1

formating date from returned mysql query

Posted: Thu Jul 13, 2006 3:14 pm
by eektech909
Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


i have a table in mysql setup with information about upcoming shows for a band
the date column has the date formatted as yyyy-mm-dd
when i display the date on the webpage from the mysql query i want it to be displayed as mm-dd-yyyy
here's the code i use (took out all the other column info)

Code: Select all

$display = mysql_query("SELECT * FROM SHOWS");

while ($row=mysql_fetch_array($display) ){
    echo($row["DATE"]);
}
can anyone help please?


Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Jul 13, 2006 3:17 pm
by Oren

Posted: Thu Jul 13, 2006 3:41 pm
by Ward
using the date() and strtotime() functions, you can format pretty much any date however you want.

Code: Select all

echo date("m-d-Y",strtotime($row["DATE"]));

Posted: Thu Jul 13, 2006 3:50 pm
by RobertGonzalez
You can look at several of the date/time formatting functions in the MySQL documentation for Date and Time Functions.

Posted: Thu Jul 13, 2006 4:08 pm
by eektech909
does anyone know how to strip the 0 from the month and date if its a single digit?

Posted: Thu Jul 13, 2006 4:13 pm
by Ward
Lookup the php date function. instead of using "m-d-Y", "m-j-Y".

Posted: Thu Jul 13, 2006 4:16 pm
by RobertGonzalez
My recommendation is to handle your date formatting on the DB. It makes the PHP script faster. The query will execute in like 0.0001 seconds with no formatting and like 0.002 with formatting. IN PHP date formatting adds a significant amount of processing time (with respect to the entire page processing time).

Posted: Thu Jul 13, 2006 4:18 pm
by RobertGonzalez
Oh yeah, your query should be something like this:

Code: Select all

SELECT DATE_FORMAT(`date_field`, '%c-%e-%Y') AS my_date FROM `mytable`;