Portál AbcLinuxu, 10. listopadu 2025 02:05
Řešení dotazu:
(tyden*7-6).měsíc.rok.
Zbytek do neděle už snadno dopočítáš.
function getDaysInWeek($week_number,$first_day_in_month,$year,$month){
$first_day = ($week_number*7)-6-$first_day_in_month;
for($x=1;$x<=7;$x++){
$first_day++;
$pole[$x] = $first_day;
}
return $pole;
}
DateTime, trošku ukecaně:
<?php
/**
* @param $year int
* @param $month int
* @param $week_offset - 1 = 1st week of month
* @param $format string - output format
* @return array
*/
function getDays($year,$month,$week_offset,$format='Y-m-d D')
{
$month = (int) $month;
$date = new DateTime();
$date->setDate($year, $month, 1);
//debug echo $date->format('Y-m-d D')."\n";
$dayOfWeek = (int) $date->format('N');
$week_offset = (int) $week_offset;
$week_offset--;
if($week_offset > 0)
{
if($dayOfWeek > 1)//to start of week
{
$dayOfWeek--;
$io = new DateInterval("P${dayOfWeek}D");
$date->sub($io);
}
$io = new DateInterval("P${week_offset}W");
$date->add($io);
}
//debug echo $date->format('Y-m-d D')."\n";
$io = new DateInterval('P1D');
$a=Array();
for($i=(int) $date->format('N');$i<8;$i++)
{
if( (int)$date->format('n') != $month)
break;
$a[] = $date->format($format);
$date->add($io);
}
return $a;
}
print_r(getDays('2011','6',1));
echo "------------\n";
print_r(getDays('2011','6',2));
echo "------------\n";
print_r(getDays('2011','6',3));
echo "------------\n";
print_r(getDays('2011','6',4));
echo "------------\n";
print_r(getDays('2011','6',5));
echo "------------\n";
print_r(getDays('2011','6',6));
echo "------------\n";
print_r(getDays('2011','6',7));
echo "------------\n";
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.