2015-11-23 50 views
1

我試圖在HTML表格中顯示日期。我想顯示每個月,但我失敗的日期時間超過一個月,例如:2015-30-11到2015-01-12用PHP顯示每個月的HTML表格中的日期

他們打破錶格,因爲有太多<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>"; 

回答

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

這樣,在每次迭代的具體日子我們對員工進行檢查即使缺席跨越幾個月,也可以在所有日期範圍內進行。

+0

完美的解決方案,我想我試過這樣做,但它得到了雜亂 – Gintoki

+0

高興地幫助:) –

0

我加入這個片段:

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

也許你應該在你的循環查看當月當前日期(環路)的是一樣的一個日期的每月通過在每個循環結束時保持一個月的最後的值($actualMonth)。如果$actualMonth爲空,您可能必須使用null在循環外部設置此值並忽略此驗證。此外,您可以在第一次循環後開始驗證。 如果不同,你必須break你的循環。您可以直接與您的$month參數進行比較。

OR

你可以限制你的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

相關問題