Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Need help in sorting date format (eg. July,2013)

    Hi,

    I have created 2 fields in a table (month & year). The month will be shown as January,February,March etc. The year will be shown as 2011,2012 etc.

    The following is the code that will capture the date format once user submit a form.

    Code:
    <?php
    
    $jd=cal_to_jd(CAL_GREGORIAN,date("m"),date("d"),date("Y"));
    $Month=jdmonthname($jd,1);
    $Year=date(Y);
    
    mysql_query ("INSERT INTO Table1 (month,year) VALUES ('$Month','$Year')");
    
    ?>
    Now, I have a problem in sorting the month in descending order (December,November,October etc). Can someone please kindly help me on the sorting and I need to make the month format remain as (December,November,October etc). Thanks.

    Below is the code to query the result, however it will sort by alphabetical order.

    Code:
    <?php
    mysql_query ("Select * from Table1 where year='$Year' order by month DESC"); ?>
    ?>

  • #2
    New Coder
    Join Date
    Feb 2012
    Posts
    40
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Think you want this

    replace:

    PHP Code:
    <?php
    mysql_query 
    ("Select * from Table1 where year='$Year' order by month DESC"); ?>
    ?>

    with this:

    PHP Code:
    <?php
    mysql_query 
    ("Select * from Table1 where year='$Year' order by month ASC"); ?>
    ?>

  • #3
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by holy24 View Post
    Hi,

    I have created 2 fields in a table (month & year). The month will be shown as January,February,March etc. The year will be shown as 2011,2012 etc.

    The following is the code that will capture the date format once user submit a form.

    Code:
    <?php
    
    $jd=cal_to_jd(CAL_GREGORIAN,date("m"),date("d"),date("Y"));
    $Month=jdmonthname($jd,1);
    $Year=date(Y);
    
    mysql_query ("INSERT INTO Table1 (month,year) VALUES ('$Month','$Year')");
    
    ?>
    Now, I have a problem in sorting the month in descending order (December,November,October etc). Can someone please kindly help me on the sorting and I need to make the month format remain as (December,November,October etc). Thanks.

    Below is the code to query the result, however it will sort by alphabetical order.

    Code:
    <?php
    mysql_query ("Select * from Table1 where year='$Year' order by month DESC"); ?>
    ?>
    Honestly, I'd store the month as an integer (1-12) and handle the conversion to a text month in the PHP/output step instead. But, if you absolutely MUST keep text month names, you can employ the ORDER BY FIELD approach which would look something like this:

    PHP Code:
    <?php
    mysql_query 
    ("SELECT * FROM Table1 WHERE year='$Year' ORDER BY FIELD (month, 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')"?>
    ?>
    That won't be as fast or efficient (or nearly as flexible) as a standard ORDER BY month ASC/DESC type of query but it will work.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •