1

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

3

3 Answers 3

1

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")));
0
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);
1
  • How will i pass the date in which format? $date if i send the date in this format 2017-03-20 it gives an error A non well formed numeric value encountered and if format like this date_create(2017-03-20) it gives error date() expects parameter 2 to be long, object given and if Commented Mar 27, 2017 at 11:39
0

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 
) 

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.