php

How to show all dates of the year weekly


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


Solution

  • 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

    I don't know why you don't want to use DateTime().

    Anyway, this is a no-datetime-version of the code:

    <?php
    
    // Set the start and end dates for the year
    $year = date('Y');
    $startDate = strtotime("$year-01-01");
    $endDate = strtotime("$year-12-31");
    
    // Initialize an array to hold weeks
    $weeks = [];
    
    // Loop through each day in the year
    for ($currentDate = $startDate; $currentDate <= $endDate; $currentDate += 86400) { // 86400 seconds in a day
        $weekNumber = date('W', $currentDate); // Get the week number
        $dayOfWeek = date('N', $currentDate); // Get the day of the week (1 = Monday, 7 = Sunday)
        $weeks[$weekNumber][$dayOfWeek] = date('Y-m-d', $currentDate);
    }
    
    // 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/82a4f