在这里得到一点帮助。 我希望生成一些代码,这些代码将接收传入的数据数组,并按时间对数据进行排序(这个我可以轻松完成)
我的表中有22块时间,所以我需要每行等于22(数组中的每个项都有一个块值)我可以这样做
我所接受的是获取该数据数据,并说如果一个数据是从上午9点开始的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
答案 0 :(得分: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 /其他)以满足您的要求。
答案 1 :(得分: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'));
}
?>