In my opinion you are going to be way better served to make all your calculation based on a unix timestamp value and then convert to string only as needed for output. That way you don't have to deal with week number problems (i.e. week 0), you are not limited to having Monday be the first day of each week (as as is the basis of calculation in date("W")
), and you won't have to make a bunch of hacks to look for edge conditions.
So assuming that $_POST['last_week']
is in your d-m-Y format something like this:
if(isset($_POST['add_week'])){
$last_week_ts = strtotime($_POST['last_week']);
$display_week_ts = $last_week_ts + (3600 * 24 * 7);
} else if (isset($_POST['back_week'])) {
$last_week_ts = strtotime($_POST['last_week']);
$display_week_ts = $last_week_ts - (3600 * 24 * 7);
} else {
$display_week_ts = floor(time() / (3600 * 24)) * 3600 * 24;
}
$week_start = date('d-m-Y', $display_week_ts);
For the part where you are looping through the week to display you can use something like this:
for ($i = 0; $i < 7; $i++) {
$current_day_ts = $display_week_ts + ($i * 3600 *24);
echo date('d-m-Y', $current_day_ts);
}