how to get sunday date between two date

前端 未结 4 366
借酒劲吻你
借酒劲吻你 2021-01-17 01:31

I try this



        
4条回答
  •  再見小時候
    2021-01-17 02:35

    Give this a try:

    $startDate = new DateTime('2016-07-15');
    $endDate = new DateTime('2016-07-17');
    
    $sundays = array();
    
    while ($startDate <= $endDate) {
        if ($startDate->format('w') == 0) {
            $sundays[] = $startDate->format('Y-m-d');
        }
        
        $startDate->modify('+1 day');
    }
    
    var_dump($sundays);
    

    If you want later to use the DateTime objects instead of the formatted date, then you must use DateTimeImmutable for the $startDate variable:

    $startDate = new DateTimeImmutable('2016-07-15');
    $endDate = new DateTimeImmutable('2016-07-17');
    
    $sundays = array();
    
    while ($startDate <= $endDate) {
        if ($startDate->format('w') == 0) {
            $sundays[] = $startDate;
        }
        
        $startDate->modify('+1 day');
    }
    
    var_dump($sundays);
    

提交回复
热议问题