View previous topic :: View next topic |
Author |
Message |
keith-f -
Joined: 27 May 2003 Posts: 18 Location: North East England
|
Posted: Fri Jun 23, 2006 8:05 am Post subject: MySQL date format |
|
|
Thanks to the volumes of help and encouragement I have received from all the contributors on this list, I now have a 44 table (Yes, 44 table!) MySQL database up and running. This coupled with the wonders of Dreamweaver8 I can INSERT UPDATE etc. data into the table fields on web pages.
The bit which has me absolutely stumped is the MySQL date handler which displays the date from a recordset as YYYY-MM-DD
I have spent countless hours on MySQL and PHP tutorials and seen some wondrous resolutions to echo the data in the desired format, but many of these involve reams and reams of code.
Am I missing something basic ? _________________ Keith
It is sometimes wiser to keep one's mouth shut and look foolish, Than to open it and remove all doubt! |
|
Back to top |
|
|
roganty -
Joined: 08 Jun 2004 Posts: 357 Location: Bristol, UK
|
Posted: Fri Jun 23, 2006 10:26 am Post subject: Re: MySQL date format |
|
|
keith-f wrote: | Thanks to the volumes of help and encouragement I have received from all the contributors on this list, I now have a 44 table (Yes, 44 table!) MySQL database up and running. This coupled with the wonders of Dreamweaver8 I can INSERT UPDATE etc. data into the table fields on web pages.
The bit which has me absolutely stumped is the MySQL date handler which displays the date from a recordset as YYYY-MM-DD
I have spent countless hours on MySQL and PHP tutorials and seen some wondrous resolutions to echo the data in the desired format, but many of these involve reams and reams of code.
Am I missing something basic ? |
When I store dates in mysql i use the unix timestamp returned by the time() function.
Then by using either getdate() or date()
I then convert the timestamp back into human readable format, eg: dd-mm-yyyy hh:mm:ss _________________ Anthony R
Roganty | Links-Links.co.uk |
|
Back to top |
|
|
aprelium -
Joined: 22 Mar 2002 Posts: 6800
|
Posted: Fri Jun 23, 2006 1:03 pm Post subject: Re: MySQL date format |
|
|
keith-f wrote: | Am I missing something basic ? |
Probably. MySQL a function that you can use in the SELECT request to format the date at will:
Code: | DATE_FORMAT(date,format) |
See http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html for more information.
Example:
Code: | SELECT DATEFORMAT(birthdate, "%W %e %M %Y") FROM friends |
This will return a list of strings of the form:
Code: | Saturday 5 October 1977 |
_________________ Support Team
Aprelium - http://www.aprelium.com |
|
Back to top |
|
|
keith-f -
Joined: 27 May 2003 Posts: 18 Location: North East England
|
Posted: Fri Jun 23, 2006 11:36 pm Post subject: |
|
|
Thanks, but this is really doin my 'Ed' in, probably because I'm as thick as buffalo dung, :? (I'm just a newbie....!)
What I have setup is a basic test page with a simple textbox and a query string
<?php echo $row_Recordset2['last_sale' ]; ?>
The SQL SELECT statement is as follows:
$query_Recordset2 = sprintf("SELECT * FROM ss_products WHERE productID = %s", $colname_Recordset2);
$Recordset2 = mysql_query($query_Recordset2, $valleyconnection) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);
The output of the query string produces 2006-06-01
I need to be able to 'humanis this to 01-06-2006
But I fear the bigger problem maybe to come when I want to UPDATE the query string from human back to SQL without the server thowing a 'Rooney' _________________ Keith
It is sometimes wiser to keep one's mouth shut and look foolish, Than to open it and remove all doubt! |
|
Back to top |
|
|
aprelium -
Joined: 22 Mar 2002 Posts: 6800
|
Posted: Sat Jun 24, 2006 4:14 pm Post subject: |
|
|
keith-f,
All you have to do is to slightly modify your SELECT statement. If ss_products has the following columns: product_name, product_qty, and product_date, your first line of code should be changed to:
Code: | $query_Recordset2 = sprintf("SELECT product_name, product_qty, DATE_FORMAT(product_date, "%d-%m-%Y") FROM ss_products WHERE productID = %s", $colname_Recordset2); |
When updating tables, you should use the MySQL function STR_TO_DATE(str,format) which can convert dates in any format to MySQL's internal date format. _________________ Support Team
Aprelium - http://www.aprelium.com |
|
Back to top |
|
|
dmacneil -
Joined: 24 Aug 2006 Posts: 1
|
Posted: Thu Aug 24, 2006 7:21 pm Post subject: |
|
|
When using the suggested query in the message above, you also have to use the following in your php code. Example:
<?php echo $row_rs_orders['DATE_FORMAT(product_date,'.$dateformat.')']; ?>
The column heading in the temporary table that is generated by the query includes DATE_FORMAT(...). Spent hours before it dawned on me. |
|
Back to top |
|
|
|