TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-19-2009, 06:49 PM   #1 (permalink)
The Contributor
 
nefus's Avatar
 
Join Date: Nov 2007
Location: Nashville, TN
Posts: 66
Thanks: 20
nefus is on a distinguished road
Default mysql order by date

So I'm wondering about order by date but I guess this probably covers most order by questions. If you do something like
Code:
 Select * from events ORDER BY date_field
Is it best to use yyyy/mm/dd or can you use dd/mm/yyyy. I assume it'll go left to right in the field and the year/month/day is the best way to sort?
__________________
I am not a programmer, nor do I play one on tv.
nefus is offline  
Reply With Quote
Old 06-19-2009, 07:51 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

If the datatype is a date type, it is not stored as a string but as a machine readable format. How it is output is decided after the sorting is done.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-23-2009, 04:40 PM   #3 (permalink)
The Contributor
 
nefus's Avatar
 
Join Date: Nov 2007
Location: Nashville, TN
Posts: 66
Thanks: 20
nefus is on a distinguished road
Default

Ok, I have no idea what you mean.
__________________
I am not a programmer, nor do I play one on tv.
nefus is offline  
Reply With Quote
Old 06-23-2009, 04:51 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

You can't change the format in which MySQL stores the date. However, you can always modify the format when you retrieve it from your table. MySQL will sort it in chronological order.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-23-2009, 05:20 PM   #5 (permalink)
The Contributor
 
nefus's Avatar
 
Join Date: Nov 2007
Location: Nashville, TN
Posts: 66
Thanks: 20
nefus is on a distinguished road
Default

Ok but my original question still seems to be about left to right or right to left in the field being ORDER BY.
__________________
I am not a programmer, nor do I play one on tv.
nefus is offline  
Reply With Quote
Old 06-23-2009, 05:31 PM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

But I don't think you can change it from YYYY/MM/DD, can you?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-23-2009, 05:33 PM   #7 (permalink)
The Contributor
 
nefus's Avatar
 
Join Date: Nov 2007
Location: Nashville, TN
Posts: 66
Thanks: 20
nefus is on a distinguished road
Default

I guess if you have inserted the data from a php statement, that was my intention. I'm sort of new to this date sorting thing and am not sure the best way to store dates really.
__________________
I am not a programmer, nor do I play one on tv.
nefus is offline  
Reply With Quote
Old 06-23-2009, 05:40 PM   #8 (permalink)
The Contributor
 
nefus's Avatar
 
Join Date: Nov 2007
Location: Nashville, TN
Posts: 66
Thanks: 20
nefus is on a distinguished road
Default

I better explain this better. I am using a javascript date picker that puts dates into a form. It uses these formats 06/23/2009 or 04.06.2009. Suggestions?
__________________
I am not a programmer, nor do I play one on tv.
nefus is offline  
Reply With Quote
Old 06-23-2009, 08:31 PM   #9 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I'd personally store the dates as a timestamp.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
nefus (06-23-2009)
Old 06-23-2009, 08:46 PM   #10 (permalink)
The Contributor
 
nefus's Avatar
 
Join Date: Nov 2007
Location: Nashville, TN
Posts: 66
Thanks: 20
nefus is on a distinguished road
Default

Yes, that does make more sense. Thanks for the suggestion, I'll change my feeble code
__________________
I am not a programmer, nor do I play one on tv.
nefus is offline  
Reply With Quote
Old 06-23-2009, 09:06 PM   #11 (permalink)
The Contributor
 
nefus's Avatar
 
Join Date: Nov 2007
Location: Nashville, TN
Posts: 66
Thanks: 20
nefus is on a distinguished road
Default losing my mind

I can't see where I am screwing this up...

Code:
echo "06/27/2009 converts to: ". strtotime("06/27/2009"), "\n";

$send_date	=	date('m/d/Y');

$send_date2	=	strtotime($send_date);

$send_date3	=	date("m/d/Y", strtotime($send_date2));
Returns this:

Code:
06/27/2009 converts to: 1246078800
send1: 06/23/2009
send2: 1245733200
send3: 12/31/1969
Where am I messing this up?? Why is strtotime not converting correctly?
__________________
I am not a programmer, nor do I play one on tv.
nefus is offline  
Reply With Quote
Old 06-23-2009, 09:55 PM   #12 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

It is because you're using strtotime again on an existing timestamp.

php Code:
$send_date  = date('m/d/Y');
printf('Date now: %s<br />', $send_date);

$send_date2 =    strtotime($send_date);
printf('As a timestamp: %s<br />', $send_date2);

$send_date3 =    date("m/d/Y", ($send_date2));
printf('And back again: %s', $send_date3);
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
nefus (06-23-2009)
Old 06-23-2009, 10:02 PM   #13 (permalink)
The Contributor
 
nefus's Avatar
 
Join Date: Nov 2007
Location: Nashville, TN
Posts: 66
Thanks: 20
nefus is on a distinguished road
Default

I feel sheepish. Thanks Wildhoney!
__________________
I am not a programmer, nor do I play one on tv.
nefus is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Keep getting mySQL error No. 1064, but i can't seem to find the problem Durux MySQL & Databases 8 04-13-2008 07:51 PM
Need more ideas for date and time class Wildhoney General 2 01-18-2008 02:22 PM
5 Useful MySQL Functions And Control Flows Wildhoney MySQL & Databases 16 01-02-2008 08:44 PM
MySQL - Is there a better way to SELECT Acrylic Absolute Beginners 3 10-24-2007 03:39 PM
converting to mysql date format harryhood Absolute Beginners 1 08-21-2005 03:57 AM


All times are GMT. The time now is 03:05 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design