spacer.png, 0 kB
spacer.png, 0 kB
spacer.png, 0 kB
Home
Week of the Month

I had need of a PHP code to tell me the current week of the month.  If it's the first week of the month, I wanted it to set the string to 1, for example.  Strangely, PHP's date() function doesn't have this; it only gives us the week of the entire year, but not the current month.

 

I found a couple sites after doing some Googling; but they all seemed to hugely over-complicate things.  One was like an entire program designed around finding the week of the month.  I rolled my eyes, and decided to do it myself.

 

Five minutes later, here is the code:

 

// Code by RobbieF.com - determines current week of the month
// March 5, 2007
$i=0;
$week=0;
if (date("N", mktime(0, 0, 0, date(n), 1, date(Y))) <= "6") $week++;
while ($i <= date(j)) {
if (date("N", mktime(0, 0, 0, date(n), $i, date(Y))) == "7") $week++;
$i++;
}

echo "Current week of the month: " . $week;

 

Feel free to use it, if you need it.  It simply sets $week to be the current week of the current month.

 

Cheers, 

- Robbie

 
spacer.png, 0 kB