Viewed   1.1k times

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:

  • Week1 = 1st to 5th
  • Week2 = 6th to 12th
  • Week3 = 13th to 19th
  • Week4 = 20th to 26th
  • Week5 = 27th to 30th

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;

 Answers

2

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

We also need to make sure that "overlapping" weeks from the previous year are handeled correctly - if January 1st is in week 52 or 53, it should be counted as week 0. (A previous version of this answer failed to do this.)

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

function weekOfYear($date) {
    $weekOfYear = intval(date("W", $date));
    if (date('n', $date) == "1" && $weekOfYear > 51) {
        // It's the last week of the previos year.
        $weekOfYear = 0;    
    }
    return $weekOfYear;
}

// A few test cases.
echo weekOfMonth(strtotime("2020-04-12")) . " "; // 2
echo weekOfMonth(strtotime("2020-12-31")) . " "; // 5
echo weekOfMonth(strtotime("2020-01-02")) . " "; // 1
echo weekOfMonth(strtotime("2021-01-28")) . " "; // 5

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

Monday, October 3, 2022
4

You probably need to specify the first day of your week with set datefirst:

set datefirst 1;
select datepart(week, '2017-02-01');

returns 6


Depending on the default language, your datefirst might be set to 7.

set datefirst 7;
select datepart(week, '2017-02-01');

returns 5

rextester demo: http://rextester.com/KEPB52852

Thursday, August 11, 2022
 
5

Try this. (Two steps.)

x <- as.Date("1/1/2016", "%d/%m/%Y")
format(x, "%G")
[1] "2015"

format(x, "%V")
[1] "53"

See ?strptime for details.

Monday, August 15, 2022
3

ISO weeks start on Monday

If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week 01. If 1 January is on a Friday, Saturday or Sunday, it is in week 52 or 53 of the previous year

Date.weekofYear= function(s){
    var yw= s.split(/D+0?/),
    weeks= 7*yw[1]-7,
    date1= new Date(yw[0], 0, 1),
    day1= date1.getDay(),
    incr= (day1> 0 && day1<5)? -1: 1;
    if(yw[2]) weeks+= (+yw[2])-1;// optional day in week

    while(date1.getDay()!= 1){
        date1.setDate(date1.getDate()+incr);
    }
    date1.setDate(date1.getDate()+weeks);
    return date1;
}

Date.weekofYear('2010-20').toLocaleDateString()
// (use UTC methods to start the weeks on Greenwich instead of local time)
/*  returned value: (String)
Monday, May 17, 2010
*/
Wednesday, October 19, 2022
2
WEEK(date[mode]); 

date = a date value.    
mode = An integer indicating the starting of the week. 

The default arugment is 0 which is sunday, setting this to 1 will be monday, 2 tuesday and so on.

week(date,1);
Sunday, September 4, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :