我正在用PHP创建一个日历,但我不能在星期六开始它,我的意思是我想要一个从星期六开始并在下周五结束的表。我卡住了:(
echo "<table class='table tableDisplay".$currentMonthNumber." table-bordered '>";
echo "<tr>
<td>Saturday</td>
<td>Sunday</td>
<td>Monday</td>
<td>Tuesday</td>
<td>Wednesday</td>
<td>Thursday</td>
<td>Friday</td>
</tr><tr>";
for ($i=1; $i <= $nbOfDaysInCurrentMonth ; $i++) {
$p = date('w', mktime(0,0,0, $calendrier_date_mois, $i, $calendrier_date_annee));
if($p == 5){
echo "</tr><tr>";
}
echo "<td>".$i."</td>";
}
echo "</table>";
答案 0 :(得分:0)
您可以使用PHP的DateTime API。我在下面为你创建了一个示例脚本,其中脚本首先检查今天是否是星期六,如果没有,那么它将使用上周六,然后循环7天。
<?php
$firstDay = null;
$isSaturday = (new DateTime("NOW"))->format('D') == 'Sat';
if($isSaturday) {
$firstDay = new DateTime("NOW");
} else {
$firstDay = new DateTime("last Saturday");
}
$days = 7;
for($i=0;$i<$days;$i++) {
echo $firstDay->format('Y-m-d').'<br />';
$firstDay->modify('+1 day');
}
答案 1 :(得分:0)
试试这个,
// Save for common reference
$today = strtotime('today');
// Get the first and last day of this month
$fdotm = strtotime('first day of this month', $today);
$ldotm = strtotime('last day of this month', $today);
// first cell (top left) of the calendar
$first = date('w', $fdotm) == 6? $fdotm: strtotime('last saturday', $fdotm);
// last cell (bottom right) of the calendar
$last = strtotime('next friday', $ldotm);
// Rendering
echo '<h2>', date('F Y', $today), '</h2>';
echo '<table>
<tr>
<th>SAT</th>
<th>SUN</th>
<th>MON</th>
<th>TUE</th>
<th>WED</th>
<th>THU</th>
<th>FRI</th>
</tr>';
for ($date = $first; $date <= $last; $date = strtotime('tomorrow', $date)){
if (date('w', $date) == 6) echo "\n<tr>\n";
$fmt = $date == $today? '<b>%s</b>': '%s';
echo '<td>', sprintf($fmt, date('j', $date)), "</td>\n";
if (date('w', $date) == 5) echo "</tr>";
}
echo "\n</table>";
希望这有帮助。