Is it possible to sort a complex for loop? Would I store the values in an array and then sort by that?
This is the for loop I'm working with, I would like to sort by the date & time of the events I'm pulling through
foreach ($recurring_events as $recurring_event):
if ($recurring_event->period == 'week') {
$StartDate = strtotime($event->RequestDateStart);
$EndDate = strtotime($event->RequestDateEnd);
$TotalDays = round(($EndDate-$StartDate)/(60*60*24*7));
for($i=0; $i<($TotalDays-1); $i++) {
$StartDate += (60*60*24*7);
if (date('WMY', $StartDate) == date('WMY')) {
echo '<div class="col-12">';
echo '<strong>Event Name:</strong> ' . $event->EventName . '<br /><strong>Date:</strong> ' . date('l, F j o',$StartDate) . '<br /><strong>Start Time:</strong> ' . date('g:i a',strtotime($event->RequestTimeStart));
echo '</div>';
}
}
}
endforeach;
It's not possible to sort the data while looping round it to output it. Instead, sort the data, then loop round the sorted data to output.
A useful function is uasort which takes an array and uses a function you define to sort it:
try this start from the end date and minus it.if you want it ti be in decsnding sorry if i dint understood you question
You need to put your results into an array, then sort it, then output it.
And then use sort
Assuming that your StartDate is the date you need (then it should be an unix timestampe?)
http://php.net/manual/en/function.sort.php
put it in an array, then refer to this post for how to sort by date
How to sort a date array in PHP