Find what week of the month a date is in PHP

{EAV_BLOG_VER:cf05756ccfd5297d}

The need for this routine comes up every now and then and it confuses people because they thing date(“W”) will do the trick.

Once they realize that’s the week of the year, they start to get all elaborate in finding ways to get which week in the month it is.

Applying a little logic, you can come up with the simple solution of subtracting away all of the weeks before the start of the month an the remainder is your answer

 

Like so:

$weekNum = date(“W”) – date(“W”, strtotime(date(“Y-m-01”, now()))) + 1;

(Drop the + 1 if you want it to be zero based.

  • Iiridayn

    Thank you for posting this. I’ve referenced it @ http://stackoverflow.com/a/13167228/118153, and it was helpful in saving me both time and code.

  • Steve

    I think this is even more elegant. Not sure if this was possible when the original post was created:

    $nowWeek = ceil(date(“j”)/7

    Voila!

  • Dmitry

    Sorry, Steve, you are very wrong.

  • ScratchCoder

    I dido Dmltry

    CRASH!
    It doesn’t parse.

  • Let me know if you are having this current version of this plugin.