0

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

tibin mathew
  • 2,058
  • 10
  • 41
  • 51
  • Rather depends what you consider the first day of the week to be. – Orbling Dec 03 '13 at 17:38
  • Isn't `2013-12-03` 2nd week of his month? If we say that week is from Monday to Sunday (ISO-8601 standard). – Glavić Dec 03 '13 at 17:42
  • 1
    How do you define week? What day does a week start on? If a week starts on Monday and the first of the month is a Sunday, is that week one, and the 2nd of the month is then week two? Or do you only count full weeks? If so, what week do you then count the 1st as if it’s a Sunday? You need to solidly define what a “week in a month” is before checking. – Martin Bean Dec 03 '13 at 18:15

2 Answers2

0

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...).

Damien - Layershift
  • 1,508
  • 8
  • 15
0

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.

dnshio
  • 914
  • 1
  • 8
  • 21