0

经过这里的一点帮助。我正在寻找一些代码来获取传入的数据数组,并按时间对数据进行排序(这我可以很容易地做到)

我的表中有 22 个时间块,所以我需要每行等于 22(数组中的每个项目都有一个块值)我可以这样做

我所追求的是获取该数据数组并说如果一条数据是从上午 09:00 开始的 3 个插槽,则需要 3 个插槽(我可以这样做)但是如果上午 09:00 是当天的第一项,我需要循环首先创建 1 个块槽,直到找到数组中的第一个槽,然后如果下一个槽集在 14:00,它会填充中间的槽

即代替

test1, 2slots, 09:00, room1
test1, 1slot, 12:00, room1 
test1, 2slots, 13:00, room1
test1, 4slots, 15:00, room1
10
11
12
13
...
22

我想

1
2
3
4
test1, 2slots, 09:00, room1, 6
7
8
9
10
test1, 1slot, 12:00, room1, 11
12
test1, 2slots, 13:00, room1, 14
15
16
test1, 4slots, 15:00, room1, 20
21
22

我的功能目前是(这将返回第一个块)

if(is_array($meetrooms)){
$i = 0;
foreach($meetrooms as $valuer){ 
    $b = $valuer['num_blocks'];
    $i += $b;
    ?>
    <span><?php echo $valuer['title'] . " " . $valuer['num_blocks'] . " " . $valuer['room_name']  . " " . $i . " " . br() ;?></span>
<?php       
}
if($i != 22){ 
    while($i <= 22){ ?>
        <span><?php echo $i . br() ;?></span>
<?php
        $i++;
    }
}
}

我的数组减少了,但结构为

[0] => Array
    (
        [mr_id] => 3
        [title] => test1 meeting
        [description] => Some meeting going on here
        [staff_id] => 2454
        [room_name] => room1
        [start_date] => 2012-04-20 00:43:58
        [start_time] => 08:00:00
        [end_date] => 2012-04-20 00:43:58
        [end_time] => 09:00:00
        [num_blocks] => 2
    )

任何帮助都会很棒!!!还需要什么请询问

编辑

我现在有 90% 的路

唯一的问题是它将 while 循环之外的值连接到 while 循环。. . ???

if(is_array($meetingrooms))
{
$time = date('H:i', strtotime("07:00"));
$i = 1;
foreach($meetingrooms as $valuer)
{ 
    while ($time < $valuer['start_time'])
    { ?>
        <span><?php echo "....." . $time . " " . $i . ".....";?></span>
    <?php
        $prev = date('H:i', strtotime($time));
        $next = strtotime('+30mins', strtotime($prev));
        $time = date('H:i', $next);
        $i++;
    }
    $nummins = $valuer['num_blocks'] * 30 - 30;
    $next = strtotime('+' . $nummins . 'mins', strtotime($time));
    $time = date('H:i', $next);
    $b = $valuer['num_blocks'];
    echo  "td" . $valuer['num_blocks'];
    $i = $i + 1;
}
$timetofinish = date('H:i', strtotime("18:00"));
if ($time < $timetofinish)
{
    while ($time < $timetofinish)
    {
        echo "<span>......$time..." . $i  . "</span>";
        $prev = date('H:i', strtotime($time));
        $next = strtotime('+30mins', strtotime($prev));
        $time = date('H:i', $next); 
        $i++;
    }
}
}

它现在返回的是

.....07:00 1..... .....07:30 2..... .....08:00 3..... td2    .....09:00 5..... .....09:30 6..... .....10:00 7..... .....10:30 8..... td1     .....11:00 10..... .....11:30 11..... .....12:00 12..... td4    .....14:00 14..... .....14:30 15..... .....15:00 16..... td1......15:30...18......16:00...19......16:30...20......17:00...21......17:30...22
4

2 回答 2

0

检查此示例代码。

    <?php 
        //Sample time loop.
        $time = new DateTime('2011-11-17 08:00:00');
        $end = new DateTime('2011-11-17 20:00:00');

        while ($time < $end) {

            echo $stamp = $time->format('h:i a') . '</br>';
            $time->add(new DateInterval('PT' . 30 . 'M'));
        }
    ?>

资源:PHP foreach 和 while 循环来订购时隙

于 2014-02-26T11:03:41.180 回答
0

您是否正在尝试创建某种教室预订时间表?

date_default_timezone_set ( 'GMT' );
$meetingrooms = array ( /* ... YOUR DATA ... */ );

define ('MAX_SLOTS', 22);
define ('START_TIME', strtotime('05:00'));
define ('END_TIME', strtotime('22:00'));
$rooms = array ( "room1", "room2" );
$reserved = array();
foreach ($rooms as $room) {
    $reserved[$room] = array();
    for($t = START_TIME; $t <= END_TIME; $t = strtotime('+30mins', $t)) {
        $reserved[$room][$t] = "FREE";
    }
}

foreach ($meetingrooms as $valuer) {
   $t = strtotime($valuer['start_time']);
   while ( $t < strtotime($valuer['end_time'])) {
       $reserved[$valuer['room_name']][$t] = $valuer;
       $t += strtotime('+30mins', $t);
   }      
}

foreach ($reserved as $roomname=>$reservations) {
    echo "{$roomname}: ";
    foreach ($reservations as $t=>$valuer) {
        echo date("H:i", $t).( ($valuer === "FREE") ? "...." : $valuer['title'])." | ";
    }
    echo "\n";
}

现在您可以修改输出(添加 HTML/XML/任何内容)以满足您的要求。

于 2012-04-20T12:02:26.243 回答