Get week number in month from date in PHP?

php get week number from date
php get month from date
php date
php get first day of week from week number
php week number by date
get week of year from date php
php get year from date
php date to timestamp

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;

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

Implemented in PHP you get this:

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

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

PHP - Get week number from date, n, Numeric representation of a month, without leading zeros, 1 through 12 This has the same value as Y, except that if the ISO week number (W) belongs to idate() - Format a local time/date as integer; getdate() - Get date/time information​  The rule is that the first week of a year is the week that contains the first Thursday of the year. I personally use Zend_Date for this kind of calculation and to get the week for today is this simple.

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

    // now return
    return $w;

date - Manual, 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'  Week numbers in PHP How to get the week number from a date. To get the ISO week number (1-53), use idate('W', $timestamp) or strftime('%-V', $timestamp). To get the week number with a leading zero (01-53), use strftime('%V', $timestamp). $timestamp is a Unix timestamp, i.e. an integer value. The return value is relative to the default timezone.

The corect way is

function weekOfMonth($date) {
    $firstOfMonth = date("Y-m-01", strtotime($date));
    return intval(date("W", strtotime($date))) - intval(date("W", strtotime($firstOfMonth)));

Get week number in month from date in PHP?, This simple function allows you to find a weeks position in the month based on a given date. For instance I needed to alert the user their date  Method 2: Using strtotime() and date() function to retrieve month from any date. Using strtotime() function, to get the month from any date, we’ll follow two steps-First convert a date to its equivalent timestamp. A timestamp of a date is the representation of seconds from January 1 1970 00:00:00 UTC to that time, then,

I have created this function on my own, which seems to work correctly. In case somebody else have a better way of doing this, please share.. Here is what I have done.

function weekOfMonth($qDate) {
    $dt = strtotime($qDate);
    $day  = date('j',$dt);
    $month = date('m',$dt);
    $year = date('Y',$dt);
    $totalDays = date('t',$dt);
    $weekCnt = 1;
    $retWeek = 0;
    for($i=1;$i<=$totalDays;$i++) {
        $curDay = date("N", mktime(0,0,0,$month,$i,$year));
        if($curDay==7) {
            if($i==$day) {
                $retWeek = $weekCnt+1;
        } else {
            if($i==$day) {
                $retWeek = $weekCnt;
    return $retWeek;

echo weekOfMonth('2015-09-08') // gives me 2;

Find The Number Of A Week In A Month For A Given Date, The correct code is the following: <?php function weeks($month, $year){ $​num_of_days = date("t",  First off, from the Manual, the ‘W’ inside the date() function returns the week number for a year. Week ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) Example: 42 (the 42nd week in the year) A quick example of date(‘W’)

function getWeekOfMonth(DateTime $date) {
    $firstDayOfMonth = new DateTime($date->format('Y-m-1'));

    return ceil(($firstDayOfMonth->format('N') + $date->format('j') - 1) / 7);

Goendg solution does not work for 2016-10-31.

Get number of weeks for a given month with PHP Solutions, print strftime("Today is day %d of the month and %j of the year. Table 3-4 contains all the day and week number format characters date( ) and strftime( ) understand. There are different ways to calculate week numbers and days in a week, but be careful when interfacing with other PHP programs or your database. Get number of weeks for a given month with PHP. Question is simple, however I have difficulty to create a function that would return this value. September 2012, their is 5 weeks in this month. February 2011, their is 4 weeks in this month.

Finding the Day in a Week, Month, Year, or the Week Number in a , How to calculate ISO week numbers, convert week numbers to dates, and get the number of weeks in a year, in PHP and other programming languages. Number of days in the given month: 28 through 31: Year-----L: Whether it's a leap year: 1 if it is a leap year, 0 otherwise. o: ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)

Week numbers 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 d, m and Y will display the day, month and year respectively Input Week Object · PHP | Number of week days between two dates · HTML | <input type="week">  PHP: date function to get month of the current date. Ask Question Asked 9 years, 7 months ago. Active 1 month ago. PHP - number of month from date. Related. 3968.

How to get first day of week in PHP?, PHP Date Exercises, Practice and Solution: Write a PHP function to get start and end date of a week (by week number) of a particular year. The date () function formats a local date and time, and returns the formatted date string. date ( format, timestamp) Parameter Values. Required. Specifies the format of the outputted date string. The following characters can be used: d - The day of the month (from 01 to 31) D - A textual representation of a day (three letters)

  • Please clarify your question.
  • this is also not giving the correct result.. '2015-09-06' should be the second week not first.
  • Can you please explain why $weekCnt is initialize by 1 ?
  • If i write echo weekOfMonth('2017-10-01'); It outputs 2. I think It should 1.
  • Welcome to Stack Overflow! Please don't just throw your source code here. Be nice and try to give a nice description to your answer, so that others will like it and upvote it. See: How do I write a good answer?
  • This only gives the correct result if the first day of the month is a monday.