获取周数从4月的第一个星期一到明年4月的第一个星期一开始计算的周数,以

时间:2017-08-11 20:42:10

标签: php date calendar week-number

我正在努力实现以下目标:

出于报告目的,我们的周数从每年4月的第一个星期一开始。

我正在使用PHP以以下格式创建日历:

M T W T F S S

在那个月的每一行结束时,我想显示周数,从1开始,4月的第一个星期一将一直到第四个星期一(明年)的第一个星期一从一个人开始。

我正在努力解决这个问题 - 任何人都可以提出解决方案吗?

由于

编辑看下面我的代码,基本上我要做的不是从01月1日开始,而是从4月的第一个星期一开始:

<?php
$monthNames = Array("January", "February", "March", "April", "May",     "June", "July", 
"August", "September", "October", "November", "December");
?>

<?php
if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");
?>

<?php
$cMonth = $_REQUEST["month"];
$cYear = $_REQUEST["year"];

$prev_year = $cYear;
$next_year = $cYear;
$prev_month = $cMonth-1;
$next_month = $cMonth+1;

if ($prev_month == 0 ) {
    $prev_month = 12;
    $prev_year = $cYear - 1;
}
if ($next_month == 13 ) {
    $next_month = 1;
    $next_year = $cYear + 1;
}
?>

<table width="250">
<tr align="center">
<td bgcolor="#999999" style="color:#FFFFFF">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%" align="left">  <a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $prev_month . "&year=" . $prev_year; ?>" style="color:#FFFFFF">Previous</a></td>
<td width="50%" align="right"><a href="<?php echo $_SERVER["PHP_SELF"] . "?month=". $next_month . "&year=" . $next_year; ?>" style="color:#FFFFFF">Next</a>  </td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center">
<table width="100%" border="0" cellpadding="2" cellspacing="2">
<tr align="center">
<td colspan="8" bgcolor="#999999" style="color:#FFFFFF"><strong><?php echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
</tr>
<tr>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>M</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>T</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>W</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>T</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>F</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>S</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>S</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF"><strong>Week No</strong></td>
</tr>


<?php 
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
//$startday = $thismonth['wday'];
$startday = $thismonth['wday']-1;

$firstDateMonth = 0;

function roundToNearestW($int, $i) {
    return ceil($int / $i) * $i;
}

if ($startday == -1)
{
    $startday = 6;
}
$complete_cells = roundToNearestW($maxday+$startday,7);
for ($i=0; $i<($complete_cells); $i++) {
    if(($i % 7) == 0 )
    {
        echo "<tr>
        ";
    }
    if($i < $startday || $i >= $maxday+$startday)
    {
        echo "<td></td>
        ";
    }
    else
    {
        if(($i - $startday + 1) > $firstDateMonth)
        {
            $firstDateMonth = ($i - $startday + 1);
        }
        echo "<td align='center' valign='middle' height='20px'>". ($i - $startday + 1) . "</td>
        ";
    }
    if(($i % 7) == 6 )
    {
        $weekDate = $cYear."-".$cMonth."-".$firstDateMonth;
        $Caldate = new DateTime($weekDate);
        $week = $Caldate->format("W");
        echo "<td align='center' valign='middle' height='20px'>WK ".$week."</td>
        ";
    }
    if(($i % 7) == 6 )
    {
        echo "</tr>";
        //firstDateMonth = 0;
    }

}
?>



</table>
</td>
</tr>
</table>

以上日历的部分功劳(稍微调整一下代码):https://www.phpjabbers.com/how-to-make-a-php-calendar-php26-4.html

0 个答案:

没有答案