how to get sunday date between two date

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

I try this



        
相关标签:
4条回答
  • 2021-01-17 02:24

    This will return you all sundays between two dates.

    $startdate = '2016-05-1';
    $enddate   = '2016-05-20';
    
    function getSundays($start, $end) {
        $timestamp1 = strtotime($start);
        $timestamp2 = strtotime($end);
        $sundays    = array();
        $oneDay     = 60*60*24;
    
        for($i = $timestamp1; $i <= $timestamp2; $i += $oneDay) {
            $day = date('N', $i);
    
            // If sunday
            if($day == 7) {
                // Save sunday in format YYYY-MM-DD, if you need just timestamp
                // save only $i
                $sundays[] = date('Y-m-d', $i);
    
                // Since we know it is sunday, we can simply skip 
                // next 6 days so we get right to next sunday
                $i += 6 * $oneDay;
            }
        }
    
        return $sundays;
    }
    
    
    var_dump(getSundays($startdate, $enddate));
    
    0 讨论(0)
  • 2021-01-17 02:26

    Try this

    $start = new DateTime($startDate);
            $end = new DateTime($endDate);
    
            $sundays = [];
            while ($start->getTimestamp() != $end->getTimestamp()) {
                if ($start->format('w') == 0) {
                    $sundays[] = $start->format('Y-m-d');
                }
                $start->add('+1 DAY');
            }
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-01-17 02:37
    function getDateForSpecificDayBetweenDates($startDate, $endDate, $weekdayNumber)
    {
     $startDate = strtotime($startDate);
     $endDate = strtotime($endDate);
    
    $dateArr = array();
    
    do
    {
        if(date("w", $startDate) != $weekdayNumber)
        {
            $startDate += (24 * 3600); // add 1 day
        }
    } while(date("w", $startDate) != $weekdayNumber);
    
    
    while($startDate <= $endDate)
    {
        $dateArr[] = date('Y-m-d', $startDate);
        $startDate += (7 * 24 * 3600); // add 7 days
    }
    
    return($dateArr);
    }
      $dateArr = getDateForSpecificDayBetweenDates('2010-01-01', '2010-12-31', 0);
      print "<pre>";
      print_r($dateArr);
    

    Try out this code..

    0 讨论(0)
提交回复
热议问题