MySQL date format

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
keith-f
-


Joined: 27 May 2003
Posts: 18
Location: North East England

PostPosted: Fri Jun 23, 2006 8:05 am    Post subject: MySQL date format Reply with quote

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 View user's profile Send private message Send e-mail
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Fri Jun 23, 2006 10:26 am    Post subject: Re: MySQL date format Reply with quote

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 View user's profile Send private message Visit poster's website
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Fri Jun 23, 2006 1:03 pm    Post subject: Re: MySQL date format Reply with quote

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 View user's profile Send private message Send e-mail
keith-f
-


Joined: 27 May 2003
Posts: 18
Location: North East England

PostPosted: Fri Jun 23, 2006 11:36 pm    Post subject: Reply with quote

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 View user's profile Send private message Send e-mail
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Sat Jun 24, 2006 4:14 pm    Post subject: Reply with quote

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 View user's profile Send private message Send e-mail
dmacneil
-


Joined: 24 Aug 2006
Posts: 1

PostPosted: Thu Aug 24, 2006 7:21 pm    Post subject: Reply with quote

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 View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group