日历开始于星期六php



我正在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>";

可以使用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');
}

试试这个,

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

最新更新