ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How to sort 2 custom date fields (month - year) chronologically?

January 12, 2011 11:32am

Subscribe [3]
  • #1 / Jan 12, 2011 11:32am

    k00k

    21 posts

    I have 2 custom fields, program_month & program_year.

    Example entries for these fields would be: April & 2010

    In my template, I retrieve all of these entries and would like to sort them in chronological order, so I’d have something like:

    January 2011
    November 2010
    October 2010
    April 2010
    February 2010
    November 2009
    September 2009
    December 2008

    You get the idea…


    Thoughts on best way to accomplish? I don’t want to use a custom numeric sort field because these entries are filtered and span multiple templates/categories, so their order is really only related to these custom date fields.

  • #2 / Jan 12, 2011 11:05pm

    Sue Crocker

    26054 posts

    You’re going to need a numeric sort, since January, February, etc. won’t sort right. You’d have to do some sort of text replacement or if statement to return January instead of 01, but you get the idea..

    Does that help?

  • #3 / Jan 13, 2011 8:52am

    k00k

    21 posts

    You’re going to need a numeric sort, since January, February, etc. won’t sort right. You’d have to do some sort of text replacement or if statement to return January instead of 01, but you get the idea..

    Does that help?

    As I said, the numeric sort won’t work in this case.

    I can do this in PHP pretty easily with an associative array (Jan => 1, Feb => 2). I was wondering if EE had anything built-in or if anyone knew of any plugins that make this easier. For now, the client is just setting the actual EE date of entry to match the program’s date and that’s working ok.

  • #4 / Jan 13, 2011 5:51pm

    Ingmar

    29245 posts

    Since you are working with month names instead of numerical months, using a PHP array or the entry_dates, as you have done, would be your best option.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases