Skip to content
Advertisement

List Weeks of Year in a Month in PHP

The scenario I have here is that, I need to select a Year and Month. And I need to show the Weeks in that currently selected Month. Let me explain it here …

2017 -> Oct -> Week N ( this should be the Week of year . E.x this week is #40 for 2017.) So I need to list number of weeks that lies in Oct for this year.

Thank you.

Advertisement

Answer

Try this code: Get first day and last day of month, count week numbers and loop over it

$year = "2017";
$month = "10";
$day = "01";
$first_date = $year."-".$month."-".$day;
$last_date = date("Y-m-t",strtotime($first_date));
$first_week = date("W",strtotime($first_date));
$last_week = date("W",strtotime($last_date));
for($i=$first_week;$i<=$last_week;$i++)
{
    echo "Week #".$i.", ";
}

Note that October has total 6 weeks. 1st date on Sunday last on Tuesday

DEMO

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement