给出不同内容的表中日期之间的日期

时间:2014-05-22 14:41:51

标签: php mysql date

我有一个场景,我需要查看日期是否在开始日期和结束日期之间,然后我需要在这些日期之间的每个表单元格填充一个单词,例如continue(例如,参见下图)。 / p>

Current table layout

从数据库中提取数据,然后将开始日期和结束日期与数组日期进行比较,我可以像上面的图像一样填充表格。当我尝试在循环之间向单元格添加信息时,会出现并发症。

我用过:

if(date("Y-m-d", strtotime($test->range[$i])) > $data['sData'] && date("Y-m-d", strtotime($test->range[$i])) > $data['eData']){
    echo "<td class='col-md-6'>Continue</td>";
}

然而,这并没有提供我所追求的输出。

我目前的代码是:

$all = $db->prepare("SELECT * FROM `assignment` WHERE user_id = $user ORDER BY `sDate` ASC");
$all->execute();
$rows = $all->fetchAll(PDO::FETCH_ASSOC);


echo "<table class='table table-hover'><tr><td></td>";
for($i = 0; $i < count($test->range); $i++){

    echo "<td class='col-md-6'>" . date("d-m-Y", strtotime($test->range[$i])) . "</td>";

}

echo "</tr>";

foreach($rows as $data){
    echo "<tr><td>" . $data['name'] . "</td>";
    for($i = 0; $i < count($test->range); $i++){
        if($data['sDate'] === date("Y-m-d", strtotime($test->range[$i]))){
            echo "<td class='col-md-6'>Start</td>";
        }
        if(date("Y-m-d", strtotime($test->range[$i])) > $data['sData'] && date("Y-m-d", strtotime($test->range[$i])) > $data['eData']){
            echo "<td class='col-md-6'>Continue</td>";
        }

        if($data['eDate'] === date("Y-m-d", strtotime($test->range[$i]))){
            echo "<td class='col-md-6'>End</td>";
        }
        echo "<td class='col-md-6'></td>";
    }
}

echo "</table>"

如何在单词continue的开头和结尾之间填充表格单元格?

1 个答案:

答案 0 :(得分:1)

只需使用辅助变量吗?

foreach($rows as $data){
    echo "<tr><td>" . $data['name'] . "</td>";
    $started = false;
    for($i = 0; $i < count($test->range); $i++){
        if($data['sDate'] === date("Y-m-d", strtotime($test->range[$i]))){
            echo "<td class='col-md-6'>Start</td>";
            $started = true;
        }

        if($data['eDate'] === date("Y-m-d", strtotime($test->range[$i]))){
            echo "<td class='col-md-6'>End</td>";
            $started = false;
        }

        if($started){
            echo "<td class='col-md-6'>Continue</td>";
        }
        echo "<td class='col-md-6'></td>";
    }
}