[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3862
  • Last Modified:

PHP MySql Sorting by date.

I have a database where the last colum has a "date" inserted. I would like the date to be displayed as MM/Day/YYYY. However i would also like people to change the sorting order from asscending to descending, for that to work properly, i am needing to display my date with YYYY/MM/day, is there anyways around this. I would like to be able to sort by Year then Month, but display the results with Month, then day, then year.

Thanks.
0
ohhhnick
Asked:
ohhhnick
  • 2
1 Solution
 
snoyes_jwCommented:
If the column type is 'date', you can sort it by that column in any order you want, and just display it formatted differently.

SELECT DATE_FORMAT(dateField, '%m/%d/%Y') AS displayDate FROM theTable ORDER BY dateField DESC;

or,
SELECT dateField FROM theTable ORDER BY dateField DESC;
echo date('m/d/Y', strtotime($row['dateField']));
0
 
AlanJDMCommented:
sort the date as is in descending order

select * from tablename order by datefield desc

then in PHP chage the format...

$date = date('m/d/Y',strtotime($rec['datefield']));



Alan
0
 
AlanJDMCommented:
Sorry snoyes... didn't see you post before sending mine.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now