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!

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.

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. 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.

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.

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:


$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
        )
        ";