两个日期之间的周数 (PHP)

Weeks between two dates (PHP)

我想知道两个日期之间的周数,但我不知道该怎么做,而且我的研究导致解决方案不起作用...

$startDate = new DateTime("2015-01-23");
$endDate = new DateTime("2015-06-29");

提前谢谢你:)

使用diff & 根据天数计算周数 -

$interval = $startDate->diff($endDate);

echo (int)(($interval->days) / 7);

输出

22

输入: DateToArray('2016-05-01', '2016-05-30');

public function DateToArray($dateTimeBegin, $dateTimeEnd)
{
    $dateToArray = DateUtils::GetDateListDateRange($dateTimeBegin, $dateTimeEnd);
    $total_weeks = array('1', '2', '3', '4','5','6');
    $week = array();
    foreach($total_weeks as $week_no):
        //intialize with null always but it is safer to initialize string when you output directly.  My Personal Preference
        $week[$week_no] = array('Sunday' => '','Monday' => '','Tuesday' => '','Wednesday' => '','Thursday' => '','Friday' => '','Saturday' => '' );
    endforeach;
    $current_week = 1;
    foreach ($dateToArray as $cdate):
        $day_of_week = date('l', strtotime( $cdate ) );
        if ($day_of_week == 'Sunday' and Date('j', strtotime($cdate)) !== '1'  ):
            $current_week ++;
        endif;
        $week[$current_week][ $day_of_week ] = date('j', strtotime( $cdate ) );
    endforeach;
    foreach($total_weeks as $week_no):
        if ($week_no > $current_week) unset ($week[$week_no]);
    endforeach;

    echo '<pre>';
    print_r($week);
    echo '</pre>';
    exit();
}

输出:

Array
(
    [1] => Array
        (
            [Sunday] => 1
            [Monday] => 2
            [Tuesday] => 3
            [Wednesday] => 4
            [Thursday] => 5
            [Friday] => 6
            [Saturday] => 7
        )

    [2] => Array
        (
            [Sunday] => 8
            [Monday] => 9
            [Tuesday] => 10
            [Wednesday] => 11
            [Thursday] => 12
            [Friday] => 13
            [Saturday] => 14
        )

    [3] => Array
        (
            [Sunday] => 15
            [Monday] => 16
            [Tuesday] => 17
            [Wednesday] => 18
            [Thursday] => 19
            [Friday] => 20
            [Saturday] => 21
        )

    [4] => Array
        (
            [Sunday] => 22
            [Monday] => 23
            [Tuesday] => 24
            [Wednesday] => 25
            [Thursday] => 26
            [Friday] => 27
            [Saturday] => 28
        )

    [5] => Array
        (
            [Sunday] => 29
            [Monday] => 30
            [Tuesday] => 
            [Wednesday] => 
            [Thursday] => 
            [Friday] => 
            [Saturday] => 
        )

)