-1

我怎样才能将星期的第一列中的第一天更改为星期日。

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

<?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>
4

2 回答 2

0

首先,更改您在表中指定的列(放置<!--i change this to Sunday-->. 添加<th>Su</th>after<td><?php echo $dYear; ?></td>并删除最后一个<th>Tu</th>before </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>您在该月的第一天之前和该月的第一天之后插入的空白 s 的数量。

// 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。您需要$tdString = '';在开始 for() 循环之前添加,如下所示:

function InsertBlankTd($numberOfTdsToAdd) {
   $tdString = '';
   for($i=1;$i<=$numberOfTdsToAdd;$i++) {
   // ... the rest of the code
于 2013-09-17T04:32:03.137 回答
0

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

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

快速编辑:

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

于 2013-09-17T04:28:48.580 回答