Get Week Number in Month from Date in PHP

Get week number in month from date in PHP?

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

PHP get number of week for month

The most frustrating thing I have ever tried to get working - but here it is!

<?php

/**
* Returns the amount of weeks into the month a date is
* @param $date a YYYY-MM-DD formatted date
* @param $rollover The day on which the week rolls over
*/
function getWeeks($date, $rollover)
{
$cut = substr($date, 0, 8);
$daylen = 86400;

$timestamp = strtotime($date);
$first = strtotime($cut . "00");
$elapsed = ($timestamp - $first) / $daylen;

$weeks = 1;

for ($i = 1; $i <= $elapsed; $i++)
{
$dayfind = $cut . (strlen($i) < 2 ? '0' . $i : $i);
$daytimestamp = strtotime($dayfind);

$day = strtolower(date("l", $daytimestamp));

if($day == strtolower($rollover)) $weeks ++;
}

return $weeks;
}

//
echo getWeeks("2011-06-11", "sunday"); //outputs 2, for the second week of the month
?>

Determine Week number from a date(Month) using PHP

All you need is

ceil(date('d')/7);

So your function will look like

function getWeekday($date){
return ceil(date('d',strtotime($date))/7);
}

Demo

Even though requirement is a little strange. Week of the month is not a well defined thing but according to your comments on various answers all you need is to see week 1 if the date is within 1st 7 days of the month. 2 for next 7, 3 for next 7, 4 for next 7 and 5 for the leftovers.

Output

D   W
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 2
9 2
10 2
11 2
12 2
13 2
14 2
15 3
16 3
17 3
18 3
19 3
20 3
21 3
22 4
23 4
24 4
25 4
26 4
27 4
28 4
29 5
30 5
31 5

Old answer

Simple! you need a capital W, not a lowercase one.

W

ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)

return date('W',strtotime($date));

The lowercase one - which you are using - is

Numeric representation of the day of the week

Manual

get week number and day from date

$input = new \DateTime('2017-07-17');
$firstDayOfMonth = new \DateTime($input->format('Y-m-01'));
$order = (int)(($input->format('j') - 1) / 7) + 1;

function ordinal($number) {
$ends = array('th','st','nd','rd','th','th','th','th','th','th');
if ((($number % 100) >= 11) && (($number%100) <= 13))
return $number. 'th';
else
return $number. $ends[$number % 10];
}

echo ordinal($order).' '.$input->format('l');

You can tinker with the code at https://3v4l.org/dg5Xa

PHP - Get week number from date

Try 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;
}

You can see it here here

You must execute as

$datee = "2016/08/10";
$datee = strtotime( str_replace("/", "-", $datee));
echo weekOfMonth($datee);

Get month value from week number using php

If you got the year and the week it is easy done with the PHP native DateTime class.

echo (new DateTime())->setISODate(2017, 5)->format('m');

This should solve your issue.

How to find week date ranges for a given month

Here is one method that loops with strtotime.

I add 86400*7 on Unix to add a week.

$month = 2;
$year = 2017;

$week = date("W", strtotime($year . "-" . $month ."-01")); // weeknumber of first day of month

Echo date("d/m/Y", strtotime($year . "-" . $month ."-01")) ." - "; // first day of month
$unix = strtotime($year."W".$week ."+1 week");
While(date("m", $unix) == $month){ // keep looping/output of while it's correct month

Echo date("d/m/Y", $unix-86400) . "\n"; // Sunday of previous week
Echo date("d/m/Y", $unix) ." - "; // this week's monday
$unix = $unix + (86400*7);
}
Echo date("d/m/Y", strtotime("last day of ".$year . "-" . $month)); //echo last day of month

https://3v4l.org/LAMBl

how to get week of month?

After some hard time looking for it, Here is the Answer, Sorry for posting a bit late here. I hope it will help you guys.

public static function getWeeksOfMonth()
{
$currentYear = date('Y');
$currentMonth = date('m');

//Substitue year and month
$time = strtotime("$currentYear-$currentMonth-01");
//Got the first week number
$firstWeek = date("W", $time);

if ($currentMonth == 12)
$currentYear++;
else
$currentMonth++;

$time = strtotime("$currentYear-$currentMonth-01") - 86400;
$lastWeek = date("W", $time);

$weekArr = array();

$j = 1;
for ($i = $firstWeek; $i <= $lastWeek; $i++) {
$weekArr[$i] = 'week ' . $j;
$j++;
}
return $weekArr;
}

The Result will be the weeks of current Month..

Array ( 
[26] => week 1
[27] => week 2
[28] => week 3
[29] => week 4
[30] => week 5
[31] => week 6
)

Sample Image



Related Topics



Leave a reply



Submit