0

是否可以对复杂的 for 循环进行排序?我会将值存储在数组中然后按此排序吗?

这是我正在使用的 for 循环,我想按我正在处理的事件的日期和时间进行排序

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; 
4

4 回答 4

2

把它放在一个数组中,然后参考这篇文章了解如何按日期排序

如何在 PHP 中对日期数组进行排序

于 2013-02-04T23:31:08.120 回答
1

在循环输出数据时无法对数据进行排序。相反,对数据进行排序,然后循环排序后的数据以输出。

一个有用的函数是uasort,它接受一个数组并使用您定义的函数对其进行排序:

function sortEvents($a, $b) {
   if (strtotime($a->RequestDateStart) == strtotime($b->RequestDateStart)) {
      return 0;
   }
   return ($a->RequestDateStart < $b->RequestDateStart) ? -1 : 1;
}
uasort($recurring_events,'sortEvents');
于 2013-02-04T23:37:46.513 回答
1

您需要将结果放入数组中,然后对其进行排序,然后将其输出。

$output = array();
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')) { //$StartDate >= strtotime('now') &&
                $output[$StartDate] .= '<div class="col-12">';

                $output[$StartDate] .='<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));

                $output[$StartDate] .='</div>';
            }
        }
    }
endforeach; 

然后使用排序

sort($output);

假设您的 StartDate 是您需要的日期(那么它应该是一个 unix 时间戳?)

http://php.net/manual/en/function.sort.php

于 2013-02-04T23:30:12.490 回答
0
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++) {
            $EndDate -= (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;

尝试从结束日期开始并减去它。如果你想要它,我很抱歉,如果我不理解你的问题

于 2013-02-09T07:47:49.270 回答