formating date from returned mysql query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eektech909
Forum Commoner
Posts: 34
Joined: Fri Jun 09, 2006 3:59 pm

formating date from returned mysql query

Post 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]
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Ward
Forum Commoner
Posts: 74
Joined: Thu Jul 13, 2006 10:01 am

Post 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"]));
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You can look at several of the date/time formatting functions in the MySQL documentation for Date and Time Functions.
eektech909
Forum Commoner
Posts: 34
Joined: Fri Jun 09, 2006 3:59 pm

Post by eektech909 »

does anyone know how to strip the 0 from the month and date if its a single digit?
Ward
Forum Commoner
Posts: 74
Joined: Thu Jul 13, 2006 10:01 am

Post by Ward »

Lookup the php date function. instead of using "m-d-Y", "m-j-Y".
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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).
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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`;
Post Reply