【问题标题】:Display dates in a HTML table for each month with PHP使用 PHP 在 HTML 表格中显示每个月的日期
【发布时间】:2015-11-23 16:07:54
【问题描述】:

我正在尝试在 HTML 表格中显示日期。我想显示每个月,但是当日期超过一个月时我失败了,例如:2015-30-11 到 2015-01-12

他们打破了表格,因为<td> 太多了,我不知道下个月如何显示它们。

每个用户都有自己的日期。

代码:

$month = date(m);
$year  = date(Y);
$day   = date(d);


if (isset($_GET['month'])) {
    $month = $_GET['month'];
}

$days = cal_days_in_month(CAL_GREGORIAN, $month, $year);


echo "<h2>$month-$day-$year</h2>";
echo "<h3>Amount of days: $days </h3>";
$days = $days + 1;

$employee_count = 0;
$sql            = "SELECT * FROM employee WHERE inactive = 0";
$result = mysqli_query($con, $sql) or die ('Unable to execute query. ' . mysqli_error($con));
while ($row = mysqli_fetch_assoc($result)) {
    $employee_id[] = $row;
    $employee_count++;
}


echo "<table border='1'>";
echo "<tablehead>";
echo "<tr>";
for ($j = 0; $j < $days; $j++) {
    echo "<th>$j</th>";
}
echo "</tr>";
echo "</tablehead>";

for ($i = 0; $i < $employee_count; $i++) {
    echo "<tr>";

    $id = $employee_id[$i]['employee_ID'];

    $sql    = "select * from employee where inactive = 0 and employee_ID = $id";
    $result = mysqli_query($con, $sql);
    while ($row = mysqli_fetch_assoc($result)) {
        $employee[] = $row;
    }

    $name    = $employee[0]['name'];
    $surname = $employee[0]['surname'];
    echo "<td>$surname $name</td>";

    $count_absences = 0;

    $sql    = "select * from absences where employee_FK = $id";
    $result = mysqli_query($con, $sql);
    while ($row = mysqli_fetch_assoc($result)) {
        $absences[] = $row;
        $count_absences++;
    }

    $table = $days - 1;
    $minus = 0;
    for ($l = 0; $l < $table; $l++) {


        for ($y = 0; $y < $count_absences; $y++) {
            $start       = $absences[$y]['start'];
            $end         = $absences[$y]['end'];
            $dStart      = new DateTime($start);
            $dEnd        = new DateTime($end);
            $dDiff       = $dStart->diff($dEnd);
            $diff        = $dDiff->days;
            $diff        = $diff + 1;
            $date        = $start;
            $start_day   = date('d', strtotime($date));
            $start_day   = $start_day - 1;
            $start_month = date('m', strtotime($date));
            $start_day   = $start_day - 1;

            if ($start_month == $month && $start_day == $l) {
                for ($a = 0; $a < $diff; $a++) {
                    echo "<td>X</td>";
                    $l++;
                }
            }

        }
        echo "<td></td>";


        unset($employee);
    }

    echo "</tr>";
    unset($absences);
}

echo "</table>";

if ($month == 12) {
    $next = 1;
} else {
    $next = $month + 1;
}

if ($month == 1) {
    $previous = 12;
} else {
    $previous = $month - 1;
}

echo "<br>";
echo "<button type=\"button\" name=\"previous\" ><a href=\"table_sev.php?month=$previous\">Previous</a></button>";
echo "<button type=\"button\" name=\"next\" ><a href=\"table_sev.php?month=$next\">Next</a></button>";

【问题讨论】:

    标签: php html mysql loops


    【解决方案1】:

    像这样修改你的内部循环:

     for($l=0;$l<$table;$l++){
    
    
        $mark = false;
        for($y=0; $y<$count_absences; $y++){
            $start =  $absences[$y]['start'];
            $end = $absences[$y]['end'];
            $dStart = new DateTime($start);
            $dEnd  = new DateTime($end);
            $dDiff = $dStart->diff($dEnd);
            $diff = $dDiff->days + 1;     
    
            $start_day = date('d', strtotime($start)) - 1;
            $start_month = date('m', strtotime($start));
    
            $lString = $year.'-'.$month.'-'.($l+1);
            $lDate = new DateTime($lString);
            if($lDate>=$dStart && $lDate<=$dEnd){
                $mark = true;
                break;
            }
        }
       if($mark){
            echo "<td>X</td>";
        } else {
             echo "<td></td>";
        }
    
    
    
    unset($employee);
    }
    

    这样,在每次迭代中,我们将根据员工缺勤的所有日期范围检查我们为员工工作的特定日期,即使缺勤跨越数月。

    【讨论】:

    • 完美的解决方案,我想我尝试过这样做,但它变得一团糟
    【解决方案2】:

    我添加了这个sn-p:

            if ($start_month != $month) {
                echo '</tr><tr>';
            }
    

    这可能就是您所需要的,但我不确定这是否会起作用。 在任何情况下,您都需要检查您是否在不同的月份才能切换到下一行。我不确定你的代码在哪里。

    <?
    $month = date(m);
    $year  = date(Y);
    $day   = date(d);
    
    
    if (isset($_GET['month'])) {
        $month = $_GET['month'];
    }
    
    $days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    
    
    echo "<h2>$month-$day-$year</h2>";
    echo "<h3>Amount of days: $days </h3>";
    $days = $days + 1;
    
    $employee_count = 0;
    $sql            = "SELECT * FROM employee WHERE inactive = 0";
    $result = mysqli_query($con, $sql) or die ('Unable to execute query. ' . mysqli_error($con));
    while ($row = mysqli_fetch_assoc($result)) {
        $employee_id[] = $row;
        $employee_count++;
    }
    
    
    echo "<table border='1'>";
    echo "<tablehead>";
    echo "<tr>";
    for ($j = 0; $j < $days; $j++) {
        echo "<th>$j</th>";
    }
    echo "</tr>";
    echo "</tablehead>";
    
    for ($i = 0; $i < $employee_count; $i++) {
        echo "<tr>";
    
        $id = $employee_id[$i]['employee_ID'];
    
        $sql    = "select * from employee where inactive = 0 and employee_ID = $id";
        $result = mysqli_query($con, $sql);
        while ($row = mysqli_fetch_assoc($result)) {
            $employee[] = $row;
        }
    
        $name    = $employee[0]['name'];
        $surname = $employee[0]['surname'];
        echo "<td>$surname $name</td>";
    
        $count_absences = 0;
    
        $sql    = "select * from absences where employee_FK = $id";
        $result = mysqli_query($con, $sql);
        while ($row = mysqli_fetch_assoc($result)) {
            $absences[] = $row;
            $count_absences++;
        }
    
        $table = $days - 1;
        $minus = 0;
        for ($l = 0; $l < $table; $l++) {
    
    
            for ($y = 0; $y < $count_absences; $y++) {
                $start       = $absences[$y]['start'];
                $end         = $absences[$y]['end'];
                $dStart      = new DateTime($start);
                $dEnd        = new DateTime($end);
                $dDiff       = $dStart->diff($dEnd);
                $diff        = $dDiff->days;
                $diff        = $diff + 1;
                $date        = $start;
                $start_day   = date('d', strtotime($date));
                $start_day   = $start_day - 1;
                $start_month = date('m', strtotime($date));
                $start_day   = $start_day - 1;
                if ($start_month != $month) {
                    echo '</tr><tr>';
                }
                if ($start_month == $month && $start_day == $l) {
                    for ($a = 0; $a < $diff; $a++) {
                        echo "<td>X</td>";
                        $l++;
                    }
                }
    
            }
            echo "<td></td>";
    
    
            unset($employee);
        }
    
        echo "</tr>";
        unset($absences);
    }
    
    echo "</table>";
    
    if ($month == 12) {
        $next = 1;
    } else {
        $next = $month + 1;
    }
    
    if ($month == 1) {
        $previous = 12;
    } else {
        $previous = $month - 1;
    }
    
    echo "<br>";
    echo "<button type=\"button\" name=\"previous\" ><a href=\"table_sev.php?month=$previous\">Previous</a></button>";
    echo "<button type=\"button\" name=\"next\" ><a href=\"table_sev.php?month=$next\">Next</a></button>";
    

    【讨论】:

      【解决方案3】:

      也许您应该在循环中检查当前日期的月份(循环中)是否与前一个日期的月份相同,方法是在每个循环结束时保留月份的最后一个值 ($actualMonth)。如果$actualMonth 为null,您可能必须在循环外设置此值并使用null 并忽略此验证。此外,您可以在第一次循环后开始验证。 如果不同,您必须 break 您的循环。您可以直接与您的$month 参数进行比较。

      您可以将 SQL 查询限制为仅在当前显示的月份缺席。

      MySQL:

      SELECT * FROM employees WHERE MONTH(date_to_checked) = month_number;
      

      https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_month

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-23
        • 1970-01-01
        • 2020-11-29
        • 2021-11-23
        • 2018-06-22
        • 1970-01-01
        相关资源
        最近更新 更多