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

Asfandyar Khan
  • 1,677
  • 15
  • 34

3 Answers3

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")));
Sachin PATIL
  • 745
  • 1
  • 9
  • 16
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);
S M Iftakhairul
  • 1,120
  • 2
  • 19
  • 42
  • 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 – Asfandyar Khan Mar 27 '17 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

Asfandyar Khan
  • 1,677
  • 15
  • 34