PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 8, 2022

[FIXED] how to get week of month?

 March 08, 2022     mysql, php, yii     No comments   

Issue

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


Solution

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



Answered By - Asfandyar Khan
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing