Current location: Hot Scripts Forums » Programming Languages » PHP » MySQL Dates


MySQL Dates

Reply
  #1 (permalink)  
Old 04-20-06, 01:16 PM
DAL's Avatar
DAL DAL is offline
Code Master
 
Join Date: Jun 2003
Location: North East England/UK
Posts: 874
Thanks: 0
Thanked 0 Times in 0 Posts
Post MySQL Dates

Can anyone suggest reason for MySQL to store dates in a strange way?
2006-04-20

Whats that all about???

Anyway I wanted to store dates in my database and ended up just setting it up as a varchar field. I now have the problem that I cannot sort the results by date. I however can sort the dates in the format above as no one will understand. I wanted to store dates dd/mm/yy but I cant find anything on the web to supply an answer.

Any suggestions
__________________
"once upon a midnight dreary, while i pron surfed, weak and weary, over many a strange and spurious site of 'hot xxx galore'. While i clicked my fav'rite bookmark, suddenly there came a warning, and my heart was filled with mourning, mourning for my dear amour," 'Tis not possible!", i muttered, "give me back my free hardcore!" quoth the server, 404."
Reply With Quote
  #2 (permalink)  
Old 04-20-06, 01:38 PM
alane alane is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 71
Thanks: 6
Thanked 0 Times in 0 Posts
Dal you can reformat the date virtually any way you wish using the DATE_FORMAT command.

For example one of the querry$ I use is:
IF ($table == 'manufacturer') {$query = "SELECT model, dir, file, version, manufacturer, DATE_FORMAT(dated, '%d %b %y') AS date FROM make, manuals WHERE (LEFT(manufacturer,3) = '$find') AND makeid = make.id ";}

This produces a date in the "09 Jan 06" format however by varying the combination within the DATE_FORMAT() there are almost limitless conbinations.

Check out- http://dev.mysql.com/doc/refman/5.0/...functions.html
Reply With Quote
  #3 (permalink)  
Old 04-20-06, 01:49 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Alane answered the - what to do about this problem...

For the "Whats that all about???" question, as you have discovered, has to do with sorting. Sorting a yyyy-mm-dd date works as expected and also allows date comparisons.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #4 (permalink)  
Old 04-21-06, 02:52 AM
0o0o0 0o0o0 is offline
Wannabe Coder
 
Join Date: Jul 2005
Posts: 213
Thanks: 0
Thanked 0 Times in 0 Posts
how about.. like ..
Sunday April 29th 2006

is that possible?

Ive been trying to figure something out for a nightclubs venue list..

example:

I add in all the upcoming dates for the month.. and when that date has passed an IF statement more or less says.. is it past this date? yup.. goto else and echo " thanks for all who came out to join us"

Any ideas on how to build something like this? most importantly with the dating described above.
Reply With Quote
  #5 (permalink)  
Old 04-21-06, 04:21 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Actually, your post does not contain enough information about what you want, for anyone to provide anything more than a guess.
Quote:
how about.. like ..
Sunday April 29th 2006

is that possible?
Is it possible to do what with this? Convert a string entered like your's into a mysql format date? Yes - see the STR_TO_DATE function at the link alane provided in the post above.

This part of the query would look something like this - STR_TO_DATE('Sunday April 29th 2006', '%W %M %D %Y')

You can put this conversion any place in the query where you want a mysql format date out of your date string.

If you have dates stored in the database in mysql format in a field called date_col and your date is in a variable called $datestring, the following should work(I have not personally used this) in a query to find all the rows where the date is less then your date -
WHERE date_col < STR_TO_DATE('$datestring', '%W %M %D %Y')

Is is possible to take a mysql format date and output it in your format? Yes - see the DATE_FORMAT function at the link alane provided.

This part of the query in a SELECT would look something like this (assuming that the dates are in a field called date_col) -
DATE_FORMAT(date_col, '%W %M %D %Y') AS date
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #6 (permalink)  
Old 04-22-06, 07:00 AM
DAL's Avatar
DAL DAL is offline
Code Master
 
Join Date: Jun 2003
Location: North East England/UK
Posts: 874
Thanks: 0
Thanked 0 Times in 0 Posts
Talking

Thanks to all for your help - most appricated!
__________________
"once upon a midnight dreary, while i pron surfed, weak and weary, over many a strange and spurious site of 'hot xxx galore'. While i clicked my fav'rite bookmark, suddenly there came a warning, and my heart was filled with mourning, mourning for my dear amour," 'Tis not possible!", i muttered, "give me back my free hardcore!" quoth the server, 404."
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
MySql Labyrinth moron Database 4 02-22-08 02:46 AM
ASP< MySQL and Dates! gillweb ASP 1 04-05-06 05:23 PM
MySQL check dates and send a mail when one year old Oskare100 PHP 1 12-23-05 04:29 AM
Using Dates - MySql & PHP dihan PHP 15 02-18-04 03:38 PM


All times are GMT -5. The time now is 07:10 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.