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

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

如果xml space id == room 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

===============>>#1 票数: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 />';
}

===============>>#2 票数: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

  ask by sloga translate from so

未解决问题?本站智能推荐: