SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Rotate array elements based on current month / day

    Hello,

    I have an array with elements representing different events. I need to rotate those based on the current month / day so that upcoming events are right on top of the list while the past ones are at the bottom moving forward as the month / day of the event is getting closer.

    The year is not relevant and also the format of the date is for demonstration purposes only.

    array(
    'event1' => array('21-june'),
    'event2' => array('12-july'),
    'event3' => array('07-november'),
    'event4' => array('18-february'),
    'event5' => array('11-march'),
    'event6' => array('22-may'),
    ....................................................
    );

    Thank you for your help!

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Year will be relevant when you get towards December otherwise January events will show in the past, so forget that.

    What formats can the date be in then? Anything we like?

    Where is the data coming from? An external site? Xml? From a db you are querying?

    Then it might be more helpful if you can be explicit and reorder that example so we can see exactly what you want, before and after.

  3. #3
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Simplest way is to use integer values.

    21 June = 0621

    So you can format the array as 'event1' => 621, 'event2' => 721

    Then a simple asort() or sort will get them in order.

    Then it's a matter of looping over it and comparing it to today's date, which is date('md')

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Year will be relevant when you get towards December otherwise January events will show in the past, so forget that.

    What formats can the date be in then? Anything we like?

    Where is the data coming from? An external site? Xml? From a db you are querying?

    Then it might be more helpful if you can be explicit and reorder that example so we can see exactly what you want, before and after.
    Thank you for your reply. Basically it'll be the same events repeating year after year so January should be in the past if not current month. Data is coming from a database (WordPress) and it can be formatted any way you would think.

    As a more explicit example:

    Event for 29th of June
    Event description

    Event for 12th of July
    Event description

    Event for 24th of August
    ........

    Event for 11th of December
    ...........

    Event for 06th of January
    ...........

    And so on the events of June, July, August, December goes at the back and January, February, etc, will be on top the next beginning of the year . This will cycle each year.

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wonshikee View Post
    Simplest way is to use integer values.

    21 June = 0621

    So you can format the array as 'event1' => 621, 'event2' => 721

    Then a simple asort() or sort will get them in order.

    Then it's a matter of looping over it and comparing it to today's date, which is date('md')

    Thank you for your reply. This is how I thought about it in the first place but will this work with pagination if there's going to be more than just a few events? I am sorry but I just can't wrap my mind around this and just can't feel which way would go better.

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rem View Post
    Data is coming from a database (WordPress)
    Why not get the events in the right sequence directly from the database: you then wouldn't need to worry about rotating anything on the PHP side.
    Salathe
    Software Developer and PHP Manual Author.

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    Why not get the events in the right sequence directly from the database: you then wouldn't need to worry about rotating anything on the PHP side.
    I don't know how to build the query. Somebody did this for me and it isn't working:

    Code:
    $querystr = "(
            SELECT p.*
            FROM $wpdb->posts p, $wpdb->postmeta pm
            WHERE p.ID = pm.post_id
            AND pm.meta_key = '_start_event'
            AND p.post_type = 'eveniment'
            AND p.post_status = 'publish'
            and DATE_FORMAT(pm.meta_value, '%d' ) >= DAY(curdate())
            and DATE_FORMAT(pm.meta_value, '%m' ) >= MONTH(curdate())
            ORDER BY DATE_FORMAT(pm.meta_value, '%m' ) ASC, DATE_FORMAT(pm.meta_value, '%d' ) ASC
            limit 1000
            ) UNION (
            SELECT p.*
            FROM $wpdb->posts p, $wpdb->postmeta pm
            WHERE p.ID = pm.post_id
            AND pm.meta_key = '_start_event'
            AND p.post_type = 'eveniment'
            AND p.post_status = 'publish'
            and
            (
            DATE_FORMAT(pm.meta_value, '%m' ) < MONTH(curdate())
            or
            (
            (DATE_FORMAT(pm.meta_value, '%d' ) = MONTH(curdate()) and DATE_FORMAT(pm.meta_value, '%d' ) <= DAY(curdate()))
            )
            )
            ORDER BY (DATE_FORMAT(pm.meta_value, '%m' ) * 1) ASC, (DATE_FORMAT(pm.meta_value, '%d' ) * 1) ASC
            limit 1000
            )
            ";


Bookmarks

Posting Permissions

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