Get the curent week of a month

173 views Asked by At

I need a php code which return the current week of the day in a month.

Eg: today is 03 / 12/ 2013 , which is first week of this month

if the date becomes 10 / 12 / 2013, then the day is in second week of this month.

I need the php code which retur current week of the month, so the value will always be from 1 to 5.

Does anyone know this code to get the value.

Any help will be appreciated.

Thanks In Advance.

-- Tibin Mathew

2

There are 2 answers

0
dnshio On

This crude little code with give you a rough estimate of what week of the month it is.

$date = date("W"); // Get the week of the year
$week = ($date % (52 / 12) ); // The division remainder (modulo) of the average number of weeks per month will tell you what week of the month you are.

It works well when I tested it for today, next week, 6th, 9th week of the year and so on. But it's not accurate because number of weeks per month never evenly average out (eg: Feb).

Hope that helps. And also I'm interested in improved answers.

0
Damien - Layershift On

I guess you just divide the day by 7?

$DoM = date("j");
switch(true) {
    case $DoM <= 7:
        //this is week 1
        break;
    case $DoM <= 14:
        //this is week 2
        break;
    case $DoM <= 21:
        //this is week 3
        break;
    case $DoM <= 28:
        //this is week 4
        break;
    default:
        //only possibility left is week 5
}

OR

Did you want to consider this based on a certain start day of the week? In that case you'd have a range 1-6 (e.g. 1st is a Saturday and start of week is Sunday would mean 31st would be in week 6...).