如何使用php查找一个月中一天的周数


How to find week number of a day in a month using php

hai我发现这个PHP get number of week for month quiet很有帮助,但它在周日开始的所有月份都会出错,例如"2013-09-01"给出的周数为2(实际上是"1")。。有人知道吗??请提前分享感谢。。。。。

使用date("W","2013-09-01");。它将返回周数为01。

添加strtotime函数。工作正常。。日期("W",时间("2013-09-01");

此实用程序函数返回特定日期的月份中的星期。需要两个参数:所需日期和当月的第一天

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

示例用法

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

返回2(2016年8月的第二周)