【问题标题】:how to get sunday date between two date如何在两个日期之间获取星期天日期
【发布时间】:2016-05-24 10:43:34
【问题描述】:

我试试这个

<?php
    $startdate = '2016-07-15';
    $enddate = '2016-07-17';
    $sundays = [];
    $startweek=date("W",strtotime($startdate));
    $endweek=date("W",strtotime($enddate));
    $year=date("Y",strtotime($startdate));

    for($i=$startweek;$i<=$endweek;$i++) {
        $result=$this->getWeek($i,$year);
        if($result>$startdate && $result<$enddate) {
            $sundays[] = $result;
        }
    }
    print_r($sundays);

    public function getWeek($week, $year)
    {
       $dto = new \DateTime();
       $result = $dto->setISODate($year, $week, 0)->format('Y-m-d');
       return $result;
    }
?>

这将返回空白数组。但在两个日期之间2016-07-17 是星期日。

我的输出为2016-07-17

我推荐这个here 但是在这个链接中返回输出为 no of sunday not date。

【问题讨论】:

标签: php


【解决方案1】:

试试这个:

$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);

如果您想稍后使用 DateTime 对象而不是格式化日期,那么您必须对 $startDate 变量使用 DateTimeImmutable:

$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);

【讨论】:

  • 我只得到日期2016-07-17
  • 这是这两个日期之间唯一的周日。您预计这 3 天之间有多少个周日?
  • 我只得到2016-07-17 而不是这个Array ( [0] =&gt; DateTime Object ( [date] =&gt; 2016-07-17 00:00:00.000000 [timezone_type] =&gt; 3 [timezone] =&gt; Asia/Kolkata ) )
  • 这个逻辑在某些情况下会失败
【解决方案2】:
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);

试试这个代码..

【讨论】:

  • 这个返回没有星期天不是日期
【解决方案3】:

试试这个

$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');
        }

【讨论】:

    【解决方案4】:

    这将返回两个日期之间的所有星期日。

    $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));
    

    【讨论】:

      【解决方案5】:

      使用碳

      $arrayOfDate = [];
      $startDate = Carbon::parse($startDate)->modify('this sunday');
      $endDate = Carbon::parse($endDate);
      
      for ($date = $startDate; $date->lte($endDate); $date->addWeek()) {
          $arrayOfDate[] = $date->format('Y-m-d');
      }
      
      return $arrayOfDate;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-02
        • 2019-12-29
        • 1970-01-01
        • 2019-08-02
        相关资源
        最近更新 更多