如何获得一周的一周?

时间:2017-03-27 11:06:41

标签: php mysql yii

我想要一个包含当月数周的数组,我怎么能得到这个?我尝试了不同的方法,但没有一种方法适合我..

$month = date('m'); // it should be CURRENT MONTH Of Current Year
$year = date('y'); // Current year

$beg = (int) date('W', strtotime("$year-$month"));
$end = (int) date('W', strtotime("$year-$month"));

我使用了上面的代码但没有工作..

我想将当月的周数放入像

这样的数组中
array ('1' => 'first week of march',  '2' => '2nd week of march',  '3' => '3rd week of march',  '4' => '4th week of march',  '5' => '5th week of march' ) etc
阵列中的<1,2> 1,2,3,4,5可以是周数

3 个答案:

答案 0 :(得分:1)

要获得开始和结束的ISO周数,请使用以下代码:

$beg = (int) date('W', strtotime(date("Y-m-01")));
$end = (int) date('W', strtotime(date("Y-m-t")));

答案 1 :(得分:0)

function weeksOfMonth($month, $year){
    $num_of_days = date("t", mktime(0,0,0,$month,1,$year)); 
    $lastday = date("t", mktime(0, 0, 0, $month, 1, $year)); 
    $no_of_weeks = 0; 
    $count_weeks = 0; 
    while($no_of_weeks < $lastday){ 
        $no_of_weeks += 7; 
        $count_weeks++; 
    }
    return $count_weeks;
}
echo weeksOfMonth(3, 2017);

答案 2 :(得分:0)

经过一段艰难的时间寻找它,这是答案,很抱歉在这里发布了一点。我希望它会帮助你们。

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

结果将是本月的几周..

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

enter image description here