Home > other >  Change week dynamic in PHP for listview
Change week dynamic in PHP for listview

Time:09-17

I want to create a list where a week is displayed and the values of each day are displayed in the section of the day.

First I tried to create the dynamic week change so that you can press a button to show the week before or the next week. Now I have this code and in the output I have a week from Sunday to Saturday.

Now here is my problem, since I always have Sunday as the first day the week that is output is also always the one from Sunday, so the week before the actual week. I tried to change the week output in the variable $dow with date('N') from date('n'). Unfortunately this did not work.

How do I have to change my code to show / output the correct week from Monday to Sunday?

Note: under $ts = 0 * 86400 * 7; on the 0 you can change the week starting from the current week.

$ts = date(strtotime('last monday'));
$ts  = 0 * 86400 * 7;
$dow = date('N' , $ts);
$offset = $dow;

//the output is currently only for testing
$ts = $ts - $offset * 86400;
$week = date('W', $ts);
echo "<p>$week</p>";
for ($x=0 ; $x<7 ; $x  ,$ts  = 86400) {
    echo '<p>' . date("d.m.Y", $ts) . '</p>' ;
}

Output to the code and current week Output to the code and current week

CodePudding user response:

Try something simpler, like this?

<?php
$currentDate = date('d.m.Y', strtotime('last monday'));
for ($x=0 ; $x<7 ; $x  ) {
    echo '<p>' . $currentDate . '</p>' ;
    $currentDate = date("d.m.Y", strtotime($currentDate . '  1 day'));
}
  • Related