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
You can simplify the code just by advancing
$now
by a single day. You can get the week number withdate('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:Your array will look like
This is how I would do it:
Snippet: https://onlinephp.io/c/5a2b8