1

So I have this array:

$dates[0] = array('start'=>'2010-07-22', 'end'=>'2010-07-23');
$dates[1] = array('start'=>'2010-07-22', 'end'=>'0000-00-00');
$dates[2] = array('start'=>'2010-07-29', 'end'=>'0000-00-00');
$dates[3] = array('start'=>'2010-07-31', 'end'=>'2010-07-31');
$dates[4] = array('start'=>'2010-07-08', 'end'=>'2010-07-31');
$dates[5] = array('start'=>'2010-08-01', 'end'=>'2010-09-30');
$dates[6] = array('start'=>'2010-07-18', 'end'=>'2010-08-15');
$dates[7] = array('start'=>'2010-07-01', 'end'=>'2010-08-31');

and i have the following condition: if an element 'start' is less then today, took the element's attribute 'end' and position the element relative to the others elements 'start'

So if an event started yesterday and ends tomorrow it should appear after the events beginning today

so the resulting array is something like this:

$dates[3] = array('start'=>'2010-07-22', 'end'=>'2010-07-23');
$dates[4] = array('start'=>'2010-07-22', 'end'=>'0000-00-00');
$dates[5] = array('start'=>'2010-07-29', 'end'=>'0000-00-00');
$dates[6] = array('start'=>'2010-07-31', 'end'=>'2010-07-31');
$dates[1] = array('start'=>'2010-07-08', 'end'=>'2010-07-31');
$dates[7] = array('start'=>'2010-08-01', 'end'=>'2010-09-30');
$dates[2] = array('start'=>'2010-07-18', 'end'=>'2010-08-15');
$dates[0] = array('start'=>'2010-07-01', 'end'=>'2010-08-31');

How could i go from the sample input to the sample output?

[EDIT]

  1. I'm trying to change the output of EventList a component for Joomla
  2. I'm not a native english speaker

So, I'll try again:

if the event start date is minor than today then order the event relative to this event enddate and the others events startday

Ex:

If the event 'A' started on 2010-07-01 and finish on 2010-07-25 and event 'B' starts on 2010-07-24 and the current date is 2010-07-20 then event 'A' comes after event 'B'

If the event 'A' starts on 2010-07-20 and finish on 2010-07-25 and event 'B' starts on 2010-07-24 and the current date is 2010-07-20 then event 'B' comes after event 'A'

I 'hope' it's a little more clearer now.

2
  • I don't quite understand the requirement, but studying the input and output makes me to realize that you are just sorting by date, and giving precedence for those with an end-date. smells homework Commented Jul 20, 2010 at 0:01
  • @yclian: re: smells homework. from taking a look at the flood of php posts today all from relatively new users, I think you may be right. Finals time? Either that or PHP has started its "Testfest". Commented Jul 20, 2010 at 0:37

1 Answer 1

1

Hmmm, so if I understand correctly: sort by stardate if >=today, enddate if startdate <= today.

function _myDateSorter($a,$b){
    $date = date('Y-m-d');
    $adate = ($a['start'] >= $date) ? $a['start'] : $a['end'];
    $bdate = ($b['start'] >= $date) ? $b['start'] : $b['end'];
    if($adate =='0000-00-00') $adate = '9999-99-99';//sort after it appears
    if($bdate =='0000-00-00') $bdate = '9999-99-99';//sort after it appears
    if($adate == $bdate) return 0;
    return $adate > $bdate ? 1 : -1;
}

uasort($dates,'_myDateSorter');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.