尝试使用PHP生成周时间线

时间:2011-09-14 19:56:34

标签: php

我正在编写某种日程安排,并在日期过程中陷入困境。

我有一个时间表,从6:00h到18:30h生成5个工作日,这没问题。 我有一个用户表和一个日期表。用户可以在一周内完成多项任务。

这是数组:

Array
(
    [0] => Array
        (
            [dateStart] => 2011-09-14 13:00:00
            [dateEnd] => 2011-09-15 11:00:00
            [eventType] => 1
            [data] => test
            [taskDescription] => Vakantieverlof
            [taskColor] => ff6600
        )

    [1] => Array
        (
            [dateStart] => 2011-09-14 15:00:00
            [dateEnd] => 2011-09-14 18:00:00
            [eventType] => 3
            [data] => 
            [taskDescription] => ADV
            [taskColor] => 336600
        )

    [2] => Array
        (
            [dateStart] => 2011-09-15 16:00:00
            [dateEnd] => 2011-09-16 10:00:00
            [eventType] => 2
            [data] => 
            [taskDescription] => Ziek
            [taskColor] => ff0000
        )
)

这是通过数组的循环:

$dat=0;
while($dat<count($row->dates))
{
    $color = "cccccc";

    if(!empty($row->dates[$dat]['taskColor'])) {
        $color = $row->dates[$dat]['taskColor'];
        $desc = $row->dates[$dat]['taskDescription'];
    } else {
        $color = "cccccc";
    }
    $datNext = $dat+1;
    if($datNext >= count($row->dates)) $datNext = $dat;
    if($row->dates[$datNext]['dateStart'] >= $hourCons AND $dat >= count($row->dates))
    {
        $dat++;

    } else {
        if( $row->dates[$dat]['dateStart'] < $hourConsEnd AND $row->dates[$dat]['dateEnd'] > $hourCons )
        {
            $wpcal .= "<div class=\"fullCell\" style=\"".$transparent." background-color: #".$color.";\"></div>";

        } else {
            $wpcal .= "<div class=\"emptyCell\" style=\"\"></div>";


        }
    }
    $dat++;

}

现在我得到3个代表每个计划任务的时间线。像这样:

Screenshot

但我希望1个时间轴包含这些任务......在这种情况下,绿色任务可能与橙色重叠。

请帮助我将它串起来几天......

3 个答案:

答案 0 :(得分:0)

在循环生成<div> s之前,首先需要将数组合并为一个数组。例如,您可以使用所有“插槽”预先创建一个数组,然后使用最适用的任务填充它们。在您进行合并的foreach中,您可以应用一些业务逻辑,例如,如果任务与另一个完全重叠,则覆盖其插槽等。

答案 1 :(得分:0)

不是遍历约会数组,而是循环每天的小时数。在该循环内部,检查任何约会是否与该时间重叠,如果是,则绘制填充。

$filled_flag = false;
foreach ($row->dates as $appt) {
  if ($appt['dateStart'] < $hourConsEnd && $appt['dateEnd'] > $hourCons) {
    $filled_flag = true;
    break;
  }
}
if ($filled_flag) {
  $wpcal .= "<div class=\"fullCell\" style=\"".$transparent." background-color: #".$color.";\"></div>";
} else {
  $wpcal .= "<div class=\"emptyCell\" style=\"\"></div>";
}

答案 2 :(得分:0)

最终我的Array是每个用户对象的一部分:

stdClass Object
(
    [id] => 2
    [username] => Emminet
    [user_level] => 100
    [dates] => Array
        (
            [0] => Array
                (
                    [dateStart] => 2011-09-07 07:00:00
                    [dateEnd] => 2011-09-14 15:29:59
                    [eventType] => 3
                    [data] => Dit is dan veld 2...
                    [taskDescription] => ADV
                    [taskColor] => 336600
                )

            [1] => Array
                (
                    [dateStart] => 2011-09-14 09:00:00
                    [dateEnd] => 2011-09-14 16:29:59
                    [eventType] => 3
                    [data] => dit is veld 1
                    [taskDescription] => ADV
                    [taskColor] => 336600
                )

            [2] => Array
                (
                    [dateStart] => 2011-09-15 12:30:00
                    [dateEnd] => 2011-09-15 16:29:59
                    [eventType] => 3
                    [data] => 
                    [taskDescription] => ADV
                    [taskColor] => 336600
                )

        )

)

所以我循环遍历用户,并为每个用户循环这周,如下所示:

for($day=1; $day<=7; $day++){
    // Timeframing settings
    $startDayAt = "06:00";
    $frameAmount = 26;
    $timeFrame = 30; // minutes
    // Timeframing output
    for($i=0,$eTime = strtotime($startDayAt); $i < $frameAmount; $i++, $eTime = strtotime("+$timeFrame minutes", $eTime)) { 

        $dat=0;
        while($dat<count($row->dates))
        {
            $color = "cccccc";

            if(!empty($row->dates[$dat]['taskColor'])) {
                $color = $row->dates[$dat]['taskColor'];
                $desc = $row->dates[$dat]['taskDescription'];
            } else {
                $color = "cccccc";
            }
            $datNext = $dat+1;
            if($datNext >= count($row->dates)) $datNext = $dat;
            if($row->dates[$datNext]['dateStart'] >= $hourCons AND $dat >= count($row->dates))
            {
                $dat++;

            } else {
                if( $row->dates[$dat]['dateStart'] < $hourConsEnd AND $row->dates[$dat]['dateEnd'] > $hourCons )
                {
                    $wpcal .= "<div class=\"fullCell\" style=\"".$transparent." background-color: #".$color.";\"></div>";

                } else {
                    $wpcal .= "<div class=\"emptyCell\" style=\"\"></div>";


                }
            }
            $dat++;

        }




}           
}

这就是我的实际循环,通过日子,然后是小时,然后是用户......