Php weekly calendar with specific start day

I am using a script provided to generate a weekly calendar. What I am looking to do though is allow the user to specify which day of the week the calendar starts on. So to have a variable that holds the day the week should start on and then be able to scroll through each week starting from this day.

Here is a very basic example of what I have:

<?php
$dt = new DateTime;

if (isset($_GET['year']) && isset($_GET['week'])){
    $dt->setISODate($_GET['year'], $_GET['week']);
}else{
    $dt->setISODate($dt->format('o'), $dt->format('W'));
}
$year = $dt->format('o');
$week = $dt->format('W');
?>
<a onclick="location.href='<?php echo $_SERVER['PHP_SELF'].'?week='.($week-1).'&year='.$year; ?>'">Previous</a> <!--Previous week-->
<a onclick="location.href='<?php echo $_SERVER['PHP_SELF'].'?week='.($week+1).'&year='.$year; ?>'">Next Week</a> <!--Next week-->
<table>
    <tr>
        <td>Employee</td>
		<?php
		do{
		    echo "<td>" . $dt->format('l') . "<br>" . $dt->format('d M Y') . "</td>\n";
		    $dt->modify('+1 day');
		}while ($week == $dt->format('W'));
		?>
    </tr>
</table>

This function allows for selecting the starting day of the week. You set it using a third parameter.
where you set the ISOdate, you just need to add a third parm to set the starting day of the week.
Read up on it here and review the examples. I think this will help!
https://www.php.net/manual/en/datetime.setisodate.php

Sponsor our Newsletter | Privacy Policy | Terms of Service