Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get week number in month from date in PHP?

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;
like image 540
Asif Hussain Avatar asked Sep 16 '15 18:09

Asif Hussain


People also ask

How to get week in php?

Use strtotime() function to get the first day of week using PHP. This function returns the default time variable timestamp and then use date() function to convert timestamp date into understandable date. strtotime() Function: The strtotime() function returns the result in timestamp by parsing the time string.

How do I calculate the number of weeks between two dates in PHP?

php function week_between_two_dates($date1, $date2) { $first = DateTime::createFromFormat('m/d/Y', $date1); $second = DateTime::createFromFormat('m/d/Y', $date2); if($date1 > $date2) return week_between_two_dates($date2, $date1); return floor($first->diff($second)->days/7); } $dt1 = '1/1/2014'; $dt2 = '12/31/2014'; ...


2 Answers

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. In a similar fashion, if a day in December is in the first week of the next year, it should be counted as 53. (Previous versions of this answer failed to do this properly.)

<?php

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.
        return 0;
    }
    else if (date('n', $date) == "12" && $weekOfYear == 1) {
        // It's the first week of the next year.
        return 53;
    }
    else {
        // It's a "normal" week.
        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
echo weekOfMonth(strtotime("2018-12-31")) . " "; // 6

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

like image 86
Anders Avatar answered Oct 09 '22 20:10

Anders


You can use the function below, fully commented:

/**
 * Returns the number of week in a month for the specified date.
 *
 * @param string $date
 * @return int
 */
function weekOfMonth($date) {
    // estract date parts
    list($y, $m, $d) = explode('-', date('Y-m-d', strtotime($date)));
    
    // current week, min 1
    $w = 1;
    
    // for each day since the start of the month
    for ($i = 1; $i < $d; ++$i) {
        // if that day was a sunday and is not the first day of month
        if ($i > 1 && date('w', strtotime("$y-$m-$i")) == 0) {
            // increment current week
            ++$w;
        }
    }
    
    // now return
    return $w;
}
like image 32
Matteo Tassinari Avatar answered Oct 09 '22 18:10

Matteo Tassinari