简体   繁体   中英

Weeks between two dates (PHP)

I would like to have the number of weeks between two dates but I don't know how to do and my research lead to solutions that do not work ...

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

Thank you in advance :)

Use diff & calculate the number of weeks depending on the number of days -

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

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

Output

22

Input: 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();
}

Output:

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] => 
        )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM