我試圖在HTML表格中顯示日期。我想顯示每個月,但我失敗的日期時間超過一個月,例如:2015-30-11到2015-01-12用PHP顯示每個月的HTML表格中的日期
他們打破錶格,因爲有太多<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>";
完美的解決方案,我想我試過這樣做,但它得到了雜亂 – Gintoki
高興地幫助:) –