0

我试图能够计算给定日期(今天)特定场地的总事件。使用 24 小时时间,我尝试在 foreach 循环中查找当天的最后一个事件,然后当当天最后一个事件的 end_time 小于现在时,显示文本“今天所有预定的事件都已结束”。

我的问题是 xml 数据包括 14 天内所有房间中的所有事件,我没有得到正确的计数。我在 foreach 中有一些 if 语句来减少它....

如果 xml 空间 id == 房间 id,那么如果 xml 事件 start_date 等于今天,那么如果 ($i < $numItems) 如果事件结束时间大于现在,则显示这些事件

当满足这些条件时,我需要记录的计数......当事件发生在今天的日期并且与通过 url 传递的 roomID 位于同一个房间时。将我的 $numItems = $count($arr) 放在 foreach 计数太多记录之前并将计数放在 foreach 中总是只返回 1 的计数。

$arr = $this->data['events'];
$numItems = count($arr);
$i = 1;
foreach ($arr as $key => $reservation){
if ((int)$reservation->space_reservation->space_id == $roomID) 
{
//test today's date
        $resDT = date('m/d/y',strtotime($reservation->reservation_start_dt));
        if ($resDT == date('m/d/y'))
            {
            if ($i < $numItems)
            {
            $repst = substr($reservation->event_start_dt,11,5);
            $repet = substr($reservation->event_end_dt,11,5);

                if ( (date('H:i',strtotime($repet))) > (date('H:i')) )
                    {
                    echo '<h2>' .$reservation->event_name. '</h2> ';
                    echo '<span class="dates">' .date('m/d/y',strtotime($reservation->event_start_dt)). ' </span> ';
                    echo '<span class="times">' .date('g:i a',strtotime($repst)). ' - ';
                    echo date('g:i a',strtotime($repet)). '</span> ';
                    echo '<br />';
                    } // ends if event end date has passed

            //echo '<span style="color:#FFF200;">'.$i.'</span>';
            echo '<br />';
            }
//if $i equals last record
            elseif ($i === $numItems) {
                // test if last event ends show
                if ( (date('g:i a',strtotime($repet))) < (date('g:i: a')) ){
                echo '<span style="color:#FFF200;">All scheduled meetings/events have concluded for today. </span><br /><br />';
                }
            }
            $i++;
            } // ends if

    } // ends if
} // ends for each
4

2 回答 2

1

以下代码将打印其中之一:

  1. 仅限给定房间的今天所有未完成的事件;
  2. “今天所有预定的活动都已结束。” 如果没有了。

我添加了两个计数器供您根据需要使用:

  • $totalEventsToday - 计算给定房间今天安排的所有事件;
  • $unfinishedEventsToday - 计算给定房间今天安排的事件,其结束日期/时间尚未过去。

所以这里是代码:

$today                 = date('m/d/y');
$events                = $this->data['events'];
$totalEventsToday      = 0;
$unfinishedEventsToday = 0;

// Check all reservations/events
foreach ($events as $reservation) {

    // Process reservation/events for the given room only
    if ((int) $reservation->space_reservation->space_id == $roomID) 
    {
        // Process reservation/events for today only
        $resDT = date('m/d/y', strtotime($reservation->reservation_start_dt));
        if ($resDT == $today)
        {
            $totalEventsToday++;

            $repst = substr($reservation->event_start_dt,11,5);
            $repet = substr($reservation->event_end_dt,11,5);

            // Process unfinished event
            if ((date('H:i',strtotime($repet))) > (date('H:i')))
            {
                $unfinishedEventsToday++;

                echo '<h2>' .$reservation->event_name. '</h2> ';
                echo '<span class="dates">' .date('m/d/y',strtotime($reservation->event_start_dt)). ' </span> ';
                echo '<span class="times">' .date('g:i a',strtotime($repst)). ' - ';
                echo date('g:i a',strtotime($repet)). '</span> ';
                echo '<br />';
            } // ends if unfinished event
         } // ends if today
    } // ends if room
} // ends for each

// Test if there aren't any unfinished events left for today
if (!$unfinishedEvents){
    echo '<span style="color:#FFF200;">All scheduled meetings/events have concluded for today. </span><br /><br />';
}
于 2013-04-16T15:56:43.447 回答
0

我想我不确定你的问题出在哪里。您可以像我在下面所做的那样简单地添加一个计数器$currentEvents

$arr = $this->data['events'];
$numItems = count($arr);
$currentEvents = 0;
$i = 1;
foreach ($arr as $key => $reservation){
if ((int)$reservation->space_reservation->space_id == $roomID) 
{
//test today's date
        $resDT = date('m/d/y',strtotime($reservation->reservation_start_dt));
        if ($resDT == date('m/d/y'))
            {
            if ($i < $numItems)
            {
            $repst = substr($reservation->event_start_dt,11,5);
            $repet = substr($reservation->event_end_dt,11,5);

                if ( (date('H:i',strtotime($repet))) > (date('H:i')) )
                    {
                    $currentEvents++;
                    echo '<h2>' .$reservation->event_name. '</h2> ';
                    echo '<span class="dates">' .date('m/d/y',strtotime($reservation->event_start_dt)). ' </span> ';
                    echo '<span class="times">' .date('g:i a',strtotime($repst)). ' - ';
                    echo date('g:i a',strtotime($repet)). '</span> ';
                    echo '<br />';
                    } // ends if event end date has passed

            //echo '<span style="color:#FFF200;">'.$i.'</span>';
            echo '<br />';
            }
//if $i equals last record
            elseif ($i === $numItems) {
                // test if last event ends show
                if ( (date('g:i a',strtotime($repet))) < (date('g:i: a')) ){
                echo '<span style="color:#FFF200;">All scheduled meetings/events have concluded for today. </span><br /><br />';
                }
            }
            $i++;
            } // ends if

    } // ends if
} // ends for each
于 2013-04-16T00:04:36.263 回答