Page 1 of 1

php sort array by date and sql retrieve date date_format

Posted: Thu Sep 03, 2009 2:41 pm
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

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

Posted: Thu Sep 03, 2009 3:09 pm
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.

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

Posted: Thu Sep 03, 2009 5:30 pm
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?