gpt4 book ai didi

php - 使用 PHP 在每个月的 HTML 表中显示日期

转载 作者:行者123 更新时间:2023-11-28 23:44:06 25 4
gpt4 key购买 nike

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

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

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

enter image description here

代码:

$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>";

最佳答案

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

 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);
}

这样,在每次迭代中,我们会根据员工缺勤的所有日期跨度检查我们为员工安排的特定日期,即使缺勤跨月也是如此。

关于php - 使用 PHP 在每个月的 HTML 表中显示日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33875463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com