Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP get number of week for month

Tags:

date

php

So I have a script that returns the number of weeks in a particular month and year. How can I take a specific day from that month and determine if it is part of week 1,2,3,4 or 5 of that month?

like image 958
brenjt Avatar asked May 02 '11 04:05

brenjt


People also ask

How to get week number of given date in php?

php $ddate = "2012-10-18"; $duedt = explode("-",$ddate); $date = mktime(0, 0, 0, $duedt[2], $duedt[1],$duedt[0]); $week = (int)date('W', $date); echo "Weeknummer: ".


2 Answers

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 ?> 
like image 184
Marty Avatar answered Oct 13 '22 14:10

Marty


Edit: so much for "single line" - needed variables to avoid recomputation with the conditional. Tossed in a default argument while I was at it.

function weekOfMonth($when = null) {     if ($when === null) $when = time();     $week = date('W', $when); // note that ISO weeks start on Monday     $firstWeekOfMonth = date('W', strtotime(date('Y-m-01', $when)));     return 1 + ($week < $firstWeekOfMonth ? $week : $week - $firstWeekOfMonth); } 

Please note that weekOfMonth(strtotime('Oct 31, 2011')); will return 6; some rare months have 6 weeks in them, contrary to OP's expectation. January 2017 is another month with 6 ISO weeks - Sunday the 1st falls in the last year's week, since ISO weeks start on Monday.

For starshine531, to return a 0 indexed week of the month, change the return 1 + to return 0 + or return (int).

For Justin Stayton, for weeks starting on Sunday instead of Monday I would use strftime('%U' instead of date('W', as follows:

function weekOfMonth($when = null) {     if ($when === null) $when = time();     $week = strftime('%U', $when); // weeks start on Sunday     $firstWeekOfMonth = strftime('%U', strtotime(date('Y-m-01', $when)));     return 1 + ($week < $firstWeekOfMonth ? $week : $week - $firstWeekOfMonth); } 

For this version, 2017-04-30 is now in week 6 of April, while 2017-01-31 is now in week 5.

like image 29
Iiridayn Avatar answered Oct 13 '22 14:10

Iiridayn