php sort array by date and sql retrieve date date_format

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cdparmeter
Forum Newbie
Posts: 4
Joined: Fri Apr 17, 2009 12:51 pm

php sort array by date and sql retrieve date date_format

Post by cdparmeter »

I'm trying to build a function that will retrieve dates from an sql table, put them into an array, sort them by date, and then echo them out in order in a different date format.

so if my table looked like this
| orderid | orderedname | orderdate_ | shipdate_ |
|___1___|Bill__________|1999-12-23|1999-12-24|
|___2___|jon__________|2004-08-20|2004-08-24|
|___3___|tod__________|2007-03-13|2007-03-26|
|___4___|Jane_________|2007-03-23|2007-03-24|

$result=myzql_query(SELECT * FROM table...);
while($row=mysql_fetch_array($result)){
$shipdate=$row[shipdate];
$orderid=$row[orderid];

$id[$shipdate]=$orderid;
}

that's the easy part now I have no idea where to start to figure out how to sort them by $shipdate and then I want to echo them out with a format of March 24, 2007
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: php sort array by date and sql retrieve date date_format

Post by requinix »

Change the query. Let that do the sorting for you.

Code: Select all

SELECT * FROM table... ORDER BY shipdate ASC
As for displaying in a different format, use DATE_FORMAT.
cdparmeter
Forum Newbie
Posts: 4
Joined: Fri Apr 17, 2009 12:51 pm

Re: php sort array by date and sql retrieve date date_format

Post by cdparmeter »

that would be easy and simple but I haven't gotten it to work it always brings up and error on the line with my $row=mysql_fetch_array($result); it says it's not a valid resource id what's the exact syntax I need to use and what am I doing wrong this was my query that fails every time

SELECT * FROM table ORDER BY shipdate ASC

also I want to add a WHERE clause too and I've tried taking them both out one at a time and can't get it to work. as long as it says ORDER BY it won't work....

k I figured out the order by statement it has to come after the where clause but I haven't figured out the date_format

in general what is the order that these query's should be placed? is there one or is it just strait up memorization?
Post Reply