Get week number in month from date in PHP?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Get week number in month from date in PHP?



I have an array of random dates (not coming from MySQL). I need to group them by the week as Week1, Week2, and so on upto Week5.



What I have is this:


$dates = array('2015-09-01','2015-09-05','2015-09-06','2015-09-15','2015-09-17');



What I need is a function to get the week number of the month by providing the date.



I know that I can get the weeknumber by doing
date('W',strtotime('2015-09-01'));
but this week number is the number between year (1-52) but I need the week number of the month only, e.g. in Sep 2015 there are 5 weeks:


date('W',strtotime('2015-09-01'));



I should be able to get the week Week1 by just providing the date
e.g.


$weekNumber = getWeekNumber('2015-09-01') //output 1;
$weekNumber = getWeekNumber('2015-09-17') //output 3;





Please clarify your question.
– William Smith
Sep 16 '15 at 18:19




9 Answers
9



I think this relationship should be true (?) and come in handy:


Week of the month = Week of the year - Week of the year of first day of month + 1



Implemented in PHP you get this:


function weekOfMonth($date)
//Get the first day of the month.
$firstOfMonth = strtotime(date("Y-m-01", $date));
//Apply above formula.
return intval(date("W", $date)) - intval(date("W", $firstOfMonth)) + 1;



To get weeks that starts with sunday, simply replace both date("W", ...) with strftime("%U", ...).


date("W", ...)


strftime("%U", ...)



You can use the function below, fully commented:


/**
* Returns the number of week in a month for the specified date.
*
* @param string $date
* @return int
*/
function weekOfMonth($date)
// estract date parts
list($y, $m, $d) = explode('-', date('Y-m-d', strtotime($date)));

// current week, min 1
$w = 1;

// for each day since the start of the month
for ($i = 1; $i <= $d; ++$i)
// if that day was a sunday and is not the first day of month
if ($i > 1 && date('w', strtotime("$y-$m-$i")) == 0)
// increment current week
++$w;



// now return
return $w;





this is also not giving the correct result.. '2015-09-06' should be the second week not first.
– Asif Hussain
Sep 17 '15 at 7:25





See my edit, should work now.
– Matteo Tassinari
Sep 17 '15 at 8:11



The corect way is


function weekOfMonth($date)
$firstOfMonth = date("Y-m-01", strtotime($date));
return intval(date("W", strtotime($date))) - intval(date("W", strtotime($firstOfMonth)));



I have created this function on my own, which seems to work correctly. In case somebody else have a better way of doing this, please share.. Here is what I have done.


function weekOfMonth($qDate)
$dt = strtotime($qDate);
$day = date('j',$dt);
$month = date('m',$dt);
$year = date('Y',$dt);
$totalDays = date('t',$dt);
$weekCnt = 1;
$retWeek = 0;
for($i=1;$i<=$totalDays;$i++)
$curDay = date("N", mktime(0,0,0,$month,$i,$year));
if($curDay==7)
if($i==$day)
$retWeek = $weekCnt+1;

$weekCnt++;
else
if($i==$day)
$retWeek = $weekCnt;



return $retWeek;





echo weekOfMonth('2015-09-08') // gives me 2;





Can you please explain why $weekCnt is initialize by 1 ?
– vidhi
Jun 30 '17 at 13:45



$weekCnt





If i write echo weekOfMonth('2017-10-01'); It outputs 2. I think It should 1.
– vidhi
Jun 30 '17 at 13:47


echo weekOfMonth('2017-10-01');


function getWeekOfMonth(DateTime $date)
$firstDayOfMonth = new DateTime($date->format('Y-m-1'));

return ceil(($firstDayOfMonth->format('N') + $date->format('j') - 1) / 7);



Goendg solution does not work for 2016-10-31.



Given the time_t wday (0=Sunday through 6=Saturday) of the first of the month in firstWday, this returns the (Sunday-based) week number within the month:


firstWday


weekOfMonth = floor((dayOfMonth + firstWday - 1)/7) + 1



Translated into PHP:


function weekOfMonth($dateString)
list($year, $month, $mday) = explode("-", $dateString);
$firstWday = date("w",strtotime("$year-$month-1"));
return floor(($mday + $firstWday - 1)/7) + 1;


function weekOfMonth($strDate)
$dateArray = explode("-", $strDate);
$date = new DateTime();
$date->setDate($dateArray[0], $dateArray[1], $dateArray[2]);
return floor((date_format($date, 'j') - 1) / 7) + 1;



weekOfMonth ('2015-09-17') // returns 3





Welcome to Stack Overflow! Please don't just throw your source code here. Be nice and try to give a nice description to your answer, so that others will like it and upvote it. See: How do I write a good answer?
– sɐunıɔןɐqɐp
Jun 7 at 7:22



You can also use this simple formula for finding week of the month


$currentWeek = ceil((date("d",strtotime($today_date)) - date("w",strtotime($today_date)) - 1) / 7) + 1;



ALGORITHM :



Date = '2018-08-08' => Y-m-d


//It's easy, no need to use php function
//Let's say your date is 2017-07-02

$Date = explode("-","2017-07-02");
$DateNo = $Date[2];
$WeekNo = $DateNo / 7; // devide it with 7
if(is_float($WeekNo) == true)

$WeekNo = ceil($WeekNo); //So answer will be 1


//If value is not float then ,you got your answer directly





This only gives the correct result if the first day of the month is a monday.
– Anders
Aug 21 '17 at 13:27






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS