Reputation: 1738
I want an array of containing weeks of current month, How can i get this? I have tried different methods but none of them is working for me..
$month = date('m'); // it should be CURRENT MONTH Of Current Year
$year = date('y'); // Current year
$beg = (int) date('W', strtotime("$year-$month"));
$end = (int) date('W', strtotime("$year-$month"));
I have used the above code but not working..
I want to put those week numbers of current month into an array like
array ('1' => 'first week of march', '2' => '2nd week of march', '3' => '3rd week of march', '4' => '4th week of march', '5' => '5th week of march' ) etc
1,2,3,4,5 in array can be week number
Upvotes: 1
Views: 3377
Reputation: 1738
After some hard time looking for it, Here is the Answer, Sorry for posting a bit late here. I hope it will help you guys.
public static function getWeeksOfMonth()
{
$currentYear = date('Y');
$currentMonth = date('m');
//Substitue year and month
$time = strtotime("$currentYear-$currentMonth-01");
//Got the first week number
$firstWeek = date("W", $time);
if ($currentMonth == 12)
$currentYear++;
else
$currentMonth++;
$time = strtotime("$currentYear-$currentMonth-01") - 86400;
$lastWeek = date("W", $time);
$weekArr = array();
$j = 1;
for ($i = $firstWeek; $i <= $lastWeek; $i++) {
$weekArr[$i] = 'week ' . $j;
$j++;
}
return $weekArr;
}
The Result will be the weeks of current Month..
Array (
[26] => week 1
[27] => week 2
[28] => week 3
[29] => week 4
[30] => week 5
[31] => week 6
)
Upvotes: 0
Reputation: 1150
function weeksOfMonth($month, $year){
$num_of_days = date("t", mktime(0,0,0,$month,1,$year));
$lastday = date("t", mktime(0, 0, 0, $month, 1, $year));
$no_of_weeks = 0;
$count_weeks = 0;
while($no_of_weeks < $lastday){
$no_of_weeks += 7;
$count_weeks++;
}
return $count_weeks;
}
echo weeksOfMonth(3, 2017);
Upvotes: 0
Reputation: 745
For getting beginning and ending ISO week number use below code:
$beg = (int) date('W', strtotime(date("Y-m-01")));
$end = (int) date('W', strtotime(date("Y-m-t")));
Upvotes: 1