【问题标题】:Get dates from next week in PHP在 PHP 中获取下周的日期
【发布时间】:2013-11-23 12:04:44
【问题描述】:

我想echo 下周从 Mo,Tu,We,Th,Fr,Sa,Su 开始的日期。

我的代码现在是这样的:

$date_monday = date("Y-m-d", strtotime('next monday'));
$date_tuesday = date("Y-m-d", strtotime('next tuesday'));
$date_wednesday = date("Y-m-d", strtotime('next wednesday'));
$date_thursday = date("Y-m-d", strtotime('next thursday'));
$date_friday = date("Y-m-d", strtotime('next friday'));
$date_saturday = date("Y-m-d", strtotime('next saturday'));
$date_sunday = date("Y-m-d", strtotime('next sunday'));

问题是,例如sunday 的日期是错误的,因为下个星期日是明天,但我想要从下个星期的星期日开始的日期。

有没有办法将 PHP 日期设置为星期日并计算新日期的天数?

【问题讨论】:

    标签: php date strtotime


    【解决方案1】:

    函数 last_week_dates(){

    $startdate = "last monday";
    $day = strtotime($startdate);
    $lastday = strtotime('sunday',$day);
    $datareturn['1'][] = date('r',$day);
    $datareturn['1'][] = date('r',$lastday);
    
    
    $preday = strtotime('-7 days',$day);
    $presund = strtotime('+6 days',$preday);
    $datareturn['0'][] = date('r',$preday);
    $datareturn['0'][] = date('r',$presund);
    
    
    $futureday = strtotime('+7 days',$day);
    $futuresund = strtotime('+6 days',$futureday);
    $datareturn['0'][] = date('r',$futureday);
    $datareturn['0'][] = date('r',$futuresund);
    }
    

    $myweekdata = last_week_dates(); print_r($myweekdata);

    试试这个对你有帮助

    【讨论】:

      【解决方案2】:

      这可以通过DateTime 类轻松实现:

      $dt = new DateTime();
      // create DateTime object with current time
      
      $dt->setISODate($dt->format('o'), $dt->format('W') + 1);
      // set object to Monday on next week
      
      $periods = new DatePeriod($dt, new DateInterval('P1D'), 6);
      // get all 1day periods from Monday to +6 days
      
      $days = iterator_to_array($periods);
      // convert DatePeriod object to array
      
      print_r($days);
      // $days[0] is Monday, ..., $days[6] is Sunday
      // to format selected date do: $days[1]->format('Y-m-d');
      

      Demo

      【讨论】:

        【解决方案3】:

        告诉strtotime()你已经在'下周'的开始会这样做

        $next_week = strtotime('next week');
        $date_monday = date("Y-m-d", strtotime('monday', $next_week));
        $date_tuesday = date("Y-m-d", strtotime('tuesday', $next_week));
        $date_wednesday = date("Y-m-d", strtotime('wednesday', $next_week));
        $date_thursday = date("Y-m-d", strtotime('thursday', $next_week));
        $date_friday = date("Y-m-d", strtotime('friday', $next_week));
        $date_saturday = date("Y-m-d", strtotime('saturday', $next_week));
        $date_sunday = date("Y-m-d", strtotime('sunday', $next_week));
        

        【讨论】:

        • next week 周日 (2013-11-24) 将返回周一 (2013-12-02)。
        • @JohnBrunner:不要相信我的话,但我认为因为 PHP 默认使用公历进行这种计算;公历周从星期日开始,而不是星期一,就像 ISO-8601 标准一样。要解决这个问题,您可以将$next_week 设置为$next_week = strtotime('+7 day', strtotime(date('o-\WW-1'))); (demo) 或者更好,使用我的example。它不仅看起来更漂亮,而且功能更强大、速度更快,请参阅简单的benchmark
        【解决方案4】:

        你可以得到下周星期天和...

        $now = new DateTime();
            while ($now->format('D') != "Sun") {
            $now->modify("+1 day");
        }
        
        $mon = $now->format('d/m/Y');
        $tue = $now->modify("+1 day")->format('d/m/Y');
        $wed = $now->modify("+1 day")->format('d/m/Y');
        $thu = $now->modify("+1 day")->format('d/m/Y');
        $fri = $now->modify("+1 day")->format('d/m/Y');
        $sat = $now->modify("+1 day")->format('d/m/Y');
        $sun = $now->modify("+1 day")->format('d/m/Y');
        

        【讨论】:

        • $mon = $mon->format('d/m/Y'); 等等?但我每天都得到相同的日期(2013 年 11 月 30 日)......
        • 那行不通。 $mon、$tue 等都引用同一个对象。您需要使用 DateTimeImmutable。
        • @vascowhite 感谢您的回答。 php 5.3中可能不存在这个东西吗?
        • 你可以clone你的对象,比如this。这种方式适用于 PHP >=5.2.0,其中DateTimeImmutable 仅适用于 PHP >= 5.5.0。
        【解决方案5】:

        这是我为满足您的需求而创建的函数:

        function datesOfNextWeek() {
          $dates = array();
          $date = time();                                 // get current date.
          while (date('w', $date += 86400) != 1);         // find the next Monday.
          for ($i = 0; $i < 7; $i++) {                    // get the 7 dates from it. 
            $dates[] = date('Y-m-d', $date + $i * 86400);
          }
          return $dates;
        }
        

        注意86400 的值是24 * 60 * 60 的结果,表示 86400 秒或 1 天(24 小时 * 60 分钟 * 60 秒)。

        要在您的代码中使用它,请使用以下代码:

        list(
          $date_monday, $date_tuesday, $date_wednesday,
          $date_thursday, $date_friday, $date_saturday,
          $date_sunday
        ) = datesOfNextWeek();
        

        好了,希望对你有帮助!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-02-18
          • 1970-01-01
          • 2015-01-29
          • 1970-01-01
          • 1970-01-01
          • 2018-02-04
          • 1970-01-01
          • 2019-12-18
          相关资源
          最近更新 更多