skip to Main Content

I want to show all date of year grouped weekly and show every weekday in one row of table.
code in below was shows all date of year: (https://stackoverflow.com/a/4044939)

$now = mktime(0,0,0,1,1,2024);
$aYearLater = $now + 31536000;
$allDates = Array();

$friday = strtotime('Next Friday', strtotime('-1 Day', $now));
$saturday = strtotime('Next Saturday', strtotime('-1 Day', $now));
$sunday = strtotime('Next Sunday', strtotime('-1 Day', $now));
$monday = strtotime('Next Monday', strtotime('-1 Day', $now));
$tuesday = strtotime('Next Tuesday', strtotime('-1 Day', $now));
$wednesday = strtotime('Next Wednesday', strtotime('-1 Day', $now));
$thursday = strtotime('Next Thursday', strtotime('-1 Day', $now));

while(1){
    if($saturday > $aYearLater)
        break 1;
    $a = array('date'=>date('Y-m-d l', $saturday));
    $b = array('daynum'=>date('w', $saturday,'','','en'));
    $allDates[] = array_merge($a,$b); 
    if($sunday > $aYearLater)
        break 1;
    $a = array('date'=>date('Y-m-d l', $sunday));
    $b = array('daynum'=>date('w', $sunday,'','','en'));
    $allDates[] = array_merge($a,$b); 
    if($monday > $aYearLater)
        break 1;
    $a = array('date'=>date('Y-m-d l', $monday));
    $b = array('daynum'=>date('w', $monday,'','','en'));
    $allDates[] = array_merge($a,$b); 
    if($tuesday > $aYearLater)
        break 1;
    $a = array('date'=>date('Y-m-d l', $tuesday));
    $b = array('daynum'=>date('w', $tuesday,'','','en'));
    $allDates[] = array_merge($a,$b); 
    if($wednesday > $aYearLater)
        break 1;
    $a = array('date'=>date('Y-m-d l', $wednesday));
    $b = array('daynum'=>date('w', $wednesday,'','','en'));
    $allDates[] = array_merge($a,$b); 
    if($thursday > $aYearLater)
        break 1;
    $a = array('date'=>date('Y-m-d l', $thursday));
    $b = array('daynum'=>date('w', $thursday,'','','en'));
    $allDates[] = array_merge($a,$b); 
    if($friday > $aYearLater)
        break 1;
    $a = array('date'=>date('Y-m-d l', $friday));
    $b = array('daynum'=>date('w', $friday,'','','en'));
    $allDates[] = array_merge($a,$b); 
    $friday = strtotime('+1 Week', $friday);
    $saturday = strtotime('+1 Week', $saturday);
    $sunday = strtotime('+1 Week', $sunday);
    $monday = strtotime('+1 Week', $monday);
    $tuesday = strtotime('+1 Week', $tuesday);
    $wednesday = strtotime('+1 Week', $wednesday);
    $thursday = strtotime('+1 Week', $thursday);
}

foreach ($allDates as $k=>$v){
    echo $allDates[$k]['date'].'</br>'.$allDates[$k]['daynum'].'</br>';
}

how i grouping this dates weekly?
I dont want use DateTime in PHP.
thanks.

I searched stackoverflow but couldn’t find an answer

2

Answers


  1. You can simplify the code just by advancing $now by a single day. You can get the week number with date('W'), but know that if you want the week to start on Sunday, you have to do a bit of manipulation. Here’s some code that will advance each day, and separate the days into weeks:

    $now = mktime(0,0,0,1,1,2024);
    $aYearLater = $now + 31536000;
    $allDates = Array();
    
    while($now <= $aYearLater) {
        $date = date('Y-m-d l', $now); // Formatted date
        $daynum = date('w', $now); // Day number
        $weekNum = $daynum == 0 ?  date('o-W', strtotime("+1 day",$now)) : date('o-W', $now); // If the day number is 0, then advance it by a day to move Sunday into the "next" weeknum
        $allDates[$weekNum][] = ['date' => $date, 'daynum' => $daynum];
        $now = strtotime('+1 day', $now);
    }
    
    foreach ($allDates as $week=>$days){ // Key is the Year-Weeknum, Days is the subarray
        foreach ($days as $k =>$v ) { // Go through each child day
            echo $v['date'].'</br>'.$v['daynum'].'</br>';
        }
    }
    

    Your array will look like

    [
        "2024-01" => [
          0 => [
            "date" => "2024-01-01 Monday",
            "daynum" => "1",
          ],
          1 => [
            "date" => "2024-01-02 Tuesday",
            "daynum" => "2",
          ],
          2 => [
            "date" => "2024-01-03 Wednesday",
            "daynum" => "3",
          ],
          3 => [
            "date" => "2024-01-04 Thursday",
            "daynum" => "4",
          ],
          4 => [
            "date" => "2024-01-05 Friday",
            "daynum" => "5",
          ],
          5 => [
            "date" => "2024-01-06 Saturday",
            "daynum" => "6",
          ],
        ],
        "2024-02" => [
          0 => [
            "date" => "2024-01-07 Sunday",
            "daynum" => "0",
          ],
          1 => [
            "date" => "2024-01-08 Monday",
            "daynum" => "1",
          ],
    ....
        ]
    ]
    
    Login or Signup to reply.
  2. This is how I would do it:

    <?php
    
    // Set the start and end dates for the year
    $year = date('Y');
    $startDate = new DateTime("$year-01-01");
    $endDate = new DateTime("$year-12-31");
    
    // Create an interval of one day
    $interval = new DateInterval('P1D');
    
    // Create a period from start to end date
    $period = new DatePeriod($startDate, $interval, $endDate->add($interval));
    
    // Initialize an array to hold weeks
    $weeks = [];
    
    // Loop through each date in the period
    foreach ($period as $date) {
        $weekNumber = $date->format('W'); // Get the week number
        $dayOfWeek = $date->format('N'); // Get the day of the week (1 = Monday, 7 = Sunday)
        $weeks[$weekNumber][$dayOfWeek] = $date->format('Y-m-d');
    }
    
    // Display the dates in an HTML table
    echo '<table border="1">';
    echo '<tr><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th><th>Sunday</th></tr>';
    
    foreach ($weeks as $week) {
        echo '<tr>';
        for ($day = 1; $day <= 7; $day++) {
            echo '<td>' . ($week[$day] ?? '') . '</td>';
        }
        echo '</tr>';
    }
    
    echo '</table>';
    ?>
    

    Snippet: https://onlinephp.io/c/5a2b8

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search