Naprimjer, na jednoj stranici želimo kalendar sa svim danima i mjesecima.
Funkcija uzima kao argumente mjesec i godinu a vraća redni broj dana i skraćeni naziv (tri slova) za dan na engleskom jeziku.
function ds_getDays($year, $month) { // Mon Tue Wed Thu Fri Sat Sun $list = []; for($d=1; $d<=31; $d++) { $time = mktime(12, 0, 0, $month, $d, $year); if (date('m', $time)==$month) { $dateExplode = explode('-',date('Y-m-d-D', $time)); array_shift($dateExplode); array_shift($dateExplode); $list[] = $dateExplode; } } return $list; }
Zatim imamo funkciju koja zove prvu:
function ds_runOnMonth($year, $fn) { $list = []; for ($i=1; $i<13; $i++) { $list[] = $fn($year, $i); } return $list; }
Test output:
echo "<pre>"; print_r ( ds_runOnMonth(2022, "ds_getDays") ); echo "</pre>";
I na kraju funkcija koja vraća array sa mjesecima i danima:
function getYear($year) { $list = []; for ($i=1; $i<13; $i++) { $monthList = []; $month = $i; for($d=1; $d<=31; $d++) { $time = mktime(12, 0, 0, $month, $d, $year); if (date('m', $time)==$month) { $dateExplode = explode('-',date('Y-m-d-D', $time)); array_shift($dateExplode); array_shift($dateExplode); $dateExplode[2] = null; //add note here $monthList[$d] = $dateExplode; } } $list[$i] = $monthList; } return $list; }
Tako, da bi dobili 12.1.2022:
$year22 = getYear(2022); print_r ($year22[1][12]);
Array ( [0] => 12 [1] => Wed [2] => )