Webmaster Forum


Go Back   Webmaster Forum > Web Development > Web Design Lobby > Coding Forum
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Coding Forum Problems with your code? Let's hear about it.

   

Reply
 
LinkBack Thread Tools Display Modes
Old 12-11-2004, 03:39 AM   #1 (permalink)
Inactive
 
Join Date: 10-29-03
Posts: 249
iTrader: 0 / 0%
Latest Blog:
None

Limit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the rough
MySQL Date Ordering Problem

My dates go into the database looking like this:-

Sat Dec 11, 2004 10:15 am

and get pulled out like this:-

Sat Dec 11, 2004 10:15 am

However when i put them in descending order they order like this:-

Sat Dec 11, 2004 10:15 am
Mon Dec 13, 2004 10:15 am
Fri Dec 10, 2004 20:49 pm

How can i get them to order correctly and formatted like they are now. Here is the table:-

date varchar(35)
Limit is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-11-2004, 07:23 AM   #2 (permalink)
Contributing Member
 
Join Date: 07-11-04
Location: Pakistan
Posts: 288
iTrader: 0 / 0%
Latest Blog:
None

digitman is liked by somebodydigitman is liked by somebodydigitman is liked by somebodydigitman is liked by somebodydigitman is liked by somebody
Send a message via MSN to digitman Send a message via Yahoo to digitman
show the query you are using?
digitman is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-11-2004, 09:42 AM   #3 (permalink)
Inactive
 
Join Date: 10-29-03
Posts: 249
iTrader: 0 / 0%
Latest Blog:
None

Limit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the rough
the query is:-

SELECT * FROM ls_client_bugs ORDER BY date DESC

do you think it has anything to do with the date being stored in the database as Sat Dec 11, 2004 10:15 am, etc..
Limit is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-11-2004, 11:05 AM   #4 (permalink)
Inactive
 
Join Date: 10-29-03
Posts: 249
iTrader: 0 / 0%
Latest Blog:
None

Limit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the rough
thats the wrong one its actually:-

SELECT * FROM table2 ORDER BY dateposted DESC
Limit is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-11-2004, 11:56 AM   #5 (permalink)
Inactive
 
littleFella's Avatar
 
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
iTrader: 0 / 0%
Latest Blog:
None

littleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to behold
The problem is not with the query but with the datatype used to store the date and time. You are using data varchar(35). This is asking for trouble for at least two reasons:

- you are using a reserved word "date" for a column name. I'm not even sure why your mySQL allowed this.
- as you see you dates are sorted in alphanumeric order, not in the order in time because you use a wrong data type

To fix your problem you have some options.
Personally I would change the datatype of the field in question into the DATETIME type. You can then use PHP date formatting functions for correct display of the datetime values using days of week etc. Remember, you don't have to store data in the exact format in which you display it. I really suggest that you read up on data types.

If the above is not possible then you may add a secondary column (call it sort_date) which would store numeric date values for ever value stored in your current date varchar(35) column. So data in that
sort_date column would look something like this:

YYYYMMDDHHMM (e.g. 200412011201 for Dec 01. 2004 at 12.01PM)

and your query would then look like this:

SELECT * FROM ls_client_bugs ORDER BY sort_date DESC


However, since the free versions of mySQL has no stroed procedures the second solution would require some sacrifices in performance.
littleFella is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-11-2004, 12:06 PM   #6 (permalink)
Inactive
 
Join Date: 10-29-03
Posts: 249
iTrader: 0 / 0%
Latest Blog:
None

Limit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the rough
k i've changed it to DATETIME but how do i format the DATETIME to what i had before.

This is date bit of my code:-
Code:
echo "<tr bgcolor=\"$bgcolor\"> <td>".$displayrow['date']."</td> <td>".$displayrow['name']."</td> <td><a href='report.php?id=".$displayrow['iid']."'>".$displayrow['title']."</a></td> <td>".$displayrow['status']."</td> </tr>";

Last edited by Limit : 12-11-2004 at 12:11 PM.
Limit is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-11-2004, 12:13 PM   #7 (permalink)
Inactive
 
littleFella's Avatar
 
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
iTrader: 0 / 0%
Latest Blog:
None

littleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to behold
http://www.php.net/manual/en/function.date.php
littleFella is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-11-2004, 12:43 PM   #8 (permalink)
Inactive
 
Join Date: 10-29-03
Posts: 249
iTrader: 0 / 0%
Latest Blog:
None

Limit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the rough
changed to:-
Code:
$query = "SELECT id, name, title, status, DATE_FORMAT(date, '%a %b %d, %Y %H:%i %p') as date FROM table2";
which formats date time to what i want but it still sorts it like this:-
Thu Dec 09, 2004 19:30 PM
Fri Dec 10, 2004 19:30 PM
Fri Dec 10, 2004 19:30 PM
Mon Dec 13, 2004 19:30 PM
Sat Dec 11, 2004 19:30 PM
Limit is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-11-2004, 12:45 PM   #9 (permalink)
Inactive
 
littleFella's Avatar
 
Join Date: 06-20-04
Location: Ontario
Posts: 3,359
iTrader: 0 / 0%
Latest Blog:
None

littleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to beholdlittleFella is a splendid one to behold
this time you forgot "order by"
littleFella is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-11-2004, 12:49 PM   #10 (permalink)
Inactive
 
Join Date: 10-29-03
Posts: 249
iTrader: 0 / 0%
Latest Blog:
None

Limit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the rough
still doesn't work

Code:
$query = "SELECT id, name, title, status, DATE_FORMAT(date, '%a %b %d, %Y %H:%i %p') as date FROM table2 ORDER BY date DESC"
orders dates like this:
Thu Dec 09, 2004 19:30 PM
Sat Dec 11, 2004 19:30 PM
Mon Dec 13, 2004 19:30 PM
Fri Dec 10, 2004 19:30 PM
Fri Dec 10, 2004 19:30 PM

instead of real date posted order
Limit is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-11-2004, 12:51 PM   #11 (permalink)
Inactive
 
Join Date: 10-29-03
Posts: 249
iTrader: 0 / 0%
Latest Blog:
None

Limit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the roughLimit is a jewel in the rough
ignore that i have it working now it was because i was calling date, date again instead of date2
Limit is offline  
Add Post to del.icio.us
Reply With Quote
Go Back   Webmaster Forum > Web Development > Web Design Lobby > Coding Forum

Reply



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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help please - date posted problem suzjor Blogging Forum 1 01-25-2007 04:48 PM
Need help with date/time problem memberhits Coding Forum 0 07-02-2006 02:42 PM
Inserting date into DATETIME field MySQL Limit Coding Forum 2 02-24-2005 06:35 PM
Mysql problem hatchet Coding Forum 5 09-15-2004 02:29 PM


Sponsor Links
Get exposure! Get exposure! Find Scripts Web Hosting Directory Get exposure! SEO Blog


All times are GMT -7. The time now is 02:40 AM.
© Copyright 2008 V7 Inc