Jump to content

PHP Calendar - start week numbers counting from the week that has the first Monday in April


mds1256

Recommended Posts

I currently have the below code that I have hacked together using various sources on the internet.

 

I am trying to get it where the week numbers start counting from the week that has the first Monday in each April.

 

So when you move to the next year it will restart counting again from the week that contains the first Monday in April.

 

I have been on for hours trying to get the logic right but not making much progress - any tips on what the logic should be?

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

if (!isset($_REQUEST["month"]))
{
	$_REQUEST["month"] = date("n");
}
if (!isset($_REQUEST["year"]))
{
	$_REQUEST["year"] = date("Y");
}

$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']-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>";
	}
    
}
?>



</table>
</td>
</tr>
</table>
Link to comment
Share on other sites

To find the date of the first Monday in April, I did this:

 

//  check this year
$curr_yr = 2020;
//  start by looking for 4/1
$dd = 1;
$date_found = '';
while($date_found == '')
{
$testdt = mktime(0, 0, 0, 4, $dd, $curr_yr);
if (date('N',$testdt) == 1)  // is it a Monday?
{
  $date_found = date('m/d/y', $testdt);
  break;
}
else
  $dd++;  // bump up the calendar date to look for
}
echo "Date found is $date_found<br>";

 

I didn't try to understand what the rest of your code did.  I figure that once you know the date of the first monday in April, you can move on with that knowledge.

 

PS - Try to learn to write your scripts to do ALL YOUR PHP coding before you start outputting the HTML.

Link to comment
Share on other sites

try

function weeknum (DateTime $dt_input) 
{
    $year = $dt_input->format('Y');
    $basedate = new datetime("first monday of april $year");

    if ($dt_input < $basedate) {
        $year--;
        $basedate = new datetime("first monday of april $year");
    }

    return 1 + floor($basedate->diff($dt_input)->days / 7);
}
[/code

To test
[code]$sdate = new DateTime('2017-02-01');
$di = new DateInterval('P7D');
$dp = new DatePeriod($sdate,$di,60);

foreach ($dp as $d) {
	printf("%s WEEK : %d<br>", $d->format('D d M Y'), weeknum($d) );
}
  • Like 2
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.