在PHP中获取周数?

时间:2022-06-04 01:42:20

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.

我有一个随机日期数组(不是来自MySQL)。我需要把它们按周分为第1周、第2周等等,直到第5周。

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:

我知道我可以通过日期(‘W’,strtotime(‘2015-09-01’)获得weeknumber;但是这个星期的数字是一年之间的数字(1-52),但是我只需要一个月的星期数,例如在2015年9月有5周:

  • Week1 = 1st to 5th
  • 第1周= 1到5周
  • Week2 = 6th to 12th
  • 第2周= 6 - 12周
  • Week3 = 13th to 19th
  • 第3周= 13 - 19周
  • Week4 = 20th to 26th
  • Week4 = 20到26号。
  • Week5 = 27th to 30th
  • 第5周= 27到30周

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;

8 个解决方案

#1


12  

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:

在PHP中实现如下:

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

要得到从周日开始的几周,只需用strftime(“%U”,…)替换两个日期(“W”,…)。

#2


9  

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

#3


4  

The corect way is

corect的方法是

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

#4


2  

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;
            }
            $weekCnt++;
        } else {
            if($i==$day) {
                $retWeek = $weekCnt;
            }
        }
    }
    return $retWeek;
}


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

#5


2  

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.

Goendg解决方案在2016-10-31赛季无效。

#6


1  

Given the time_t wday (0=Sunday through 6=Saturday) of the first of the month in firstWday, this returns the (Sunday-based) week number within the month:

给定第一个月的第一天的时间t wday (0=Sunday - 6=Saturday),这将返回当月的(基于周日的)周数:

weekOfMonth = floor((dayOfMonth + firstWday - 1)/7) + 1 

Translated into PHP:

翻译成PHP:

function weekOfMonth($dateString) {
  list($year, $month, $mday) = explode("-", $dateString);
  $firstWday = date("w",strtotime("$year-$month-1"));
  return floor(($mday + $firstWday - 1)/7) + 1;
}

#7


0  

function weekOfMonth($strDate) {
  $dateArray = explode("-", $strDate);
  $date = new DateTime();
  $date->setDate($dateArray[0], $dateArray[1], $dateArray[2]);
  return floor((date_format($date, 'j') - 1) / 7) + 1;  
}

weekOfMonth ('2015-09-17') // returns 3

(“2015-09-17”)//返回3

#8


-1  

//It's easy, no need to use php function
//Let's say your date is 2017-07-02

$Date = explode("-","2017-07-02");
$DateNo = $Date[2];
$WeekNo = $DateNo / 7; // devide it with 7
if(is_float($WeekNo) == true)
{
   $WeekNo = ceil($WeekNo); //So answer will be 1
}  

//If value is not float then ,you got your answer directly

#1


12  

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:

在PHP中实现如下:

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

要得到从周日开始的几周,只需用strftime(“%U”,…)替换两个日期(“W”,…)。

#2


9  

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

#3


4  

The corect way is

corect的方法是

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

#4


2  

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;
            }
            $weekCnt++;
        } else {
            if($i==$day) {
                $retWeek = $weekCnt;
            }
        }
    }
    return $retWeek;
}


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

#5


2  

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.

Goendg解决方案在2016-10-31赛季无效。

#6


1  

Given the time_t wday (0=Sunday through 6=Saturday) of the first of the month in firstWday, this returns the (Sunday-based) week number within the month:

给定第一个月的第一天的时间t wday (0=Sunday - 6=Saturday),这将返回当月的(基于周日的)周数:

weekOfMonth = floor((dayOfMonth + firstWday - 1)/7) + 1 

Translated into PHP:

翻译成PHP:

function weekOfMonth($dateString) {
  list($year, $month, $mday) = explode("-", $dateString);
  $firstWday = date("w",strtotime("$year-$month-1"));
  return floor(($mday + $firstWday - 1)/7) + 1;
}

#7


0  

function weekOfMonth($strDate) {
  $dateArray = explode("-", $strDate);
  $date = new DateTime();
  $date->setDate($dateArray[0], $dateArray[1], $dateArray[2]);
  return floor((date_format($date, 'j') - 1) / 7) + 1;  
}

weekOfMonth ('2015-09-17') // returns 3

(“2015-09-17”)//返回3

#8


-1  

//It's easy, no need to use php function
//Let's say your date is 2017-07-02

$Date = explode("-","2017-07-02");
$DateNo = $Date[2];
$WeekNo = $DateNo / 7; // devide it with 7
if(is_float($WeekNo) == true)
{
   $WeekNo = ceil($WeekNo); //So answer will be 1
}  

//If value is not float then ,you got your answer directly