线性日历第一天是星期一,更改为星期日

时间:2013-09-17 04:14:56

标签: php

如何将第一列中的第一天更改为星期日。

这是我从网站获得的代码,我一直在尝试改变日期的位置,但它仍然显示星期一的第一天作为星期一。我怎么能把它改成星期天,提前谢谢:)

<?php

$dDaysOnPage = 37;
$dDay = 1;

if (isset($_REQUEST['year']))
{
    if ($_REQUEST['year'] <> "") 
    { 
        $dYear = $_REQUEST['year']; 
    } 
    else 
    { 
        $dYear = date("Y"); 
    }
}
else
{
    $_REQUEST['year'] = date("Y");
    $dYear = $_REQUEST['year'];
}
?>

<table style="margin-left:-80px;" class="table table-bordered">
    <tr class="blue">
        <td><?php echo $dYear; ?></td>
        <th>Mo</th>   <!--i change this to Sunday-->
        <th>Tu</th>
        <th>We</th>
        <th>Th</th>
        <th>Fr</th>
        <th>Sa</th>
        <th>Su</th>
        <th>Mo</th>
        <th>Tu</th>
        <th>We</th>
        <th>Th</th>
        <th>Fr</th>
        <th>Sa</th>
        <th>Su</th>
        <th>Mo</th>
        <th>Tu</th>
        <th>We</th>
        <th>Th</th>
        <th>Fr</th>
        <th>Sa</th>
        <th>Su</th>
        <th>Mo</th>
        <th>Tu</th>
        <th>We</th>
        <th>Th</th>
        <th>Fr</th>
        <th>Sa</th>
        <th>Su</th>
        <th>Mo</th>
        <th>Tu</th>
        <th>We</th>
        <th>Th</th>
        <th>Fr</th>
        <th>Sa</th>
        <th>Su</th>
        <th>Mo</th>
        <th>Tu</th>
    </tr>                                   
    <?php
    function FriendlyDayOfWeek($dayNum) {
    // converts the sunday to 7
    // This function can be removed in php 5 by - date("N"),
    // just remove function calls below and replace by swapping date("w" for date("N"
    if ($dayNum == 0){ return 7; } else { return $dayNum; }
    }

    function InsertBlankTd($numberOfTdsToAdd) {
    for($i=1;$i<=$numberOfTdsToAdd;$i++) {
    $tdString .= "<td></td>";
    }
    return $tdString;
    }

    for ($mC=1;$mC<=12;$mC++) {
    $currentDT = mktime(0,0,0,$mC,$dDay,$dYear);
    echo "<tr><td class='monthName'><div>".date("M",$currentDT)."</div></td>";
        $daysInMonth = date("t",$currentDT);

        echo InsertBlankTd(FriendlyDayOfWeek(date("w",$currentDT))-1);

        for ($i=1;$i<=$daysInMonth;$i++) {
        $exactDT = mktime(0,0,0,$mC,$i,$dYear);
        if ($i==date("d")&&date("m",$currentDT)==date("m")) { $class="currentDay"; } else { $class = ""; }
        echo "<td class='".$class." days day".FriendlyDayOfWeek(date("w",$exactDT))."'>".$i."</td>";
        }

        echo InsertBlankTd($dDaysOnPage - $daysInMonth - FriendlyDayOfWeek(date("w",$currentDT))+1);
        echo "</tr>";
    }
    ?>
</table>

2 个答案:

答案 0 :(得分:0)

尝试在strtotime()中包装你的$ currentDT变量,如下所示:

echo InsertBlankTd(FriendlyDayOfWeek(date("w", strtotime($currentDT . ' -1 day')))-1);

快速修改:

如果这不起作用,请尝试在打印日期的其他地方使用相同的方法。我没有测试代码...

答案 1 :(得分:0)

首先,更改您在表格中指定的列(放置<!--i change this to Sunday-->的位置。)在<th>Su</th>之后添加<td><?php echo $dYear; ?></td>并删除<th>Tu</th>之前的</tr>}像这样:

   <td><?php echo $dYear; ?></td>
   <th>Su</th>
   <th>Mo</th>   <!--i change this to Sunday-->
   <th>Tu</th>
   <!-- etc. -->
   <th>Su</th>
   <th>Mo</th>
</tr>

其次,调整您在该月的第一天之前和该月的第一天之后插入的空白<td>的数量。

// First change here, remove the -1 at the end
echo InsertBlankTd(FriendlyDayOfWeek(date("w",$currentDT)));

for ($i=1;$i<=$daysInMonth;$i++) {
$exactDT = mktime(0,0,0,$mC,$i,$dYear);
if ($i==date("d")&&date("m",$currentDT)==date("m")) { $class="currentDay"; } else { $class = ""; }
   echo "<td class='".$class." days day".FriendlyDayOfWeek(date("w",$exactDT))."'>".$i."</td>";
}

// Second change here, remove the +1 at the end
echo InsertBlankTd($dDaysOnPage - $daysInMonth - FriendlyDayOfWeek(date("w",$currentDT)));

第三,由于未定义InsertBlankTd(),函数$tdString内部会出现警告。您需要在开始for()循环之前添加$tdString = '';,如下所示:

function InsertBlankTd($numberOfTdsToAdd) {
   $tdString = '';
   for($i=1;$i<=$numberOfTdsToAdd;$i++) {
   // ... the rest of the code