0

hai i found this PHP get number of week for month quiet helpful but it makes some error on all months start on Sundays for example "2013-09-01" gives week number as 2 (actually its '1').. any one got some idea?? please share thanks in advance.....

Community
  • 1
  • 1
tradebel123
  • 435
  • 1
  • 5
  • 20
  • The comments on the accepted answer of that question indicate that the solution has bugs. Have you tried the other answers? They appear to work flawlessly. – FThompson Feb 12 '13 at 06:28
  • you are right mate...i found one from them...thanx...and sorry for the trouble – tradebel123 Feb 12 '13 at 07:15

3 Answers3

3

use date("W","2013-09-01");.it will return week number as 01.

Ripa Saha
  • 2,532
  • 6
  • 27
  • 51
1

Add strtotime function to it.. Works fine.. date("W",strtotime("2013-09-01"));

  • 1
    Doesn't this give the week number in the year? I thought the OP wanted week number of a day in a month – iamjonesy Nov 27 '15 at 14:58
1

This utility function returns the week of the month for a specific day. Needs two args: the wanted day, and the first day of the same month

class WeekCalculator
{    
    public static function getWeekOfMonth($date, $firstDayOfMonth) {    
        $w1 = date("W", $firstDayOfMonth->getTimestamp());
        $w2 = date("W", $date->getTimestamp());

        $weekNum = $w2 - $w1 + 1;
        return $weekNum;
    }
}

Example usage

WeekCalculator::getWeekOfMonth(new \DateTime('2016-8-8'), new \DateTime('2016-8-1'));

Returns 2 (second week of August 2016)

voghDev
  • 5,641
  • 2
  • 37
  • 41