【问题标题】:PHP Get recurring week from specific start datePHP从特定开始日期获取重复周
【发布时间】:2013-11-07 16:11:59
【问题描述】:

我正在考虑尝试设置和阵列,看起来像这样:

$dates = array(
    [0] => "07/11/2013",
    [1] => "14/11/2013",
    [2] => "21/11/2013",
    [3] => "28/11/2013",
    [4] => "05/12/2013",
    [5] => "12/12/2013");

我愿意使用它,但由于我希望明年再次出现这种情况,我更愿意让 PHP 执行此操作并将其输入到我的数组中。我知道如何将其限制为我想要的特定数量,但我不知道如何在当前日期或特定日期添加一周,例如,如果我想开始 08/11/2013

我快速浏览了一下,似乎找不到任何这样做的东西。

我只需要一个脚本来为当前日期添加一周,目前是每个星期四,然后将其添加到数组中。

我唯一的问题是我不确定如何指定日期,然后每次都添加一周。我认为这里最好使用for 循环。

【问题讨论】:

  • 你现在怎么样了?看看datestrtotime

标签: php arrays date


【解决方案1】:

使用DateTime 类。 DateInterval 和 DatePeriod 类是在 PHP 5.3.0 中引入的,因此以下解决方案仅适用于 PHP >= 5.3.0:

$start = new DateTime('2013-11-07');
$end = new DateTime('2013-12-31');
$interval = new DateInterval('P1W');  // one week

$p = new DatePeriod($start, $interval, $end);

foreach ($p as $w) {
    $weeks[] = $w->format('d-m-Y');
}

Demo!

正如 Glavic 在 comments below 中指出的那样,这也可以在以前的 PHP 版本中使用 modify() 方法完成:

$start = new DateTime('2013-11-07');
$end = new DateTime('2013-12-31');

$weeks = array();
while ($start < $end) {
    $weeks[] = $start->format('d-m-Y');
    $start->modify('+1 week');
}

Demo.

【讨论】:

  • 虽然是一个很好的解决方案,但这仅适用于 php 5.4 及更高版本:) 并不罕见,但如果使用较低版本可能会导致失望:)
  • @Martijn:是的,但是 DateTime 是最好的。我在顶部添加了解释。干杯:)
  • 我已经看到了一些使用以下答案的示例,但是,我目前正在运行 5.4.7,这非常适合我的需求,谢谢!
  • @Martijn,AmalMurali:DateInterval 和 DatePeriod 类是在 PHP 5.3.0 中引入的;不知道你在哪里发现它只适用于 >=5.4.0?这段代码可以修改,所以它可以在 PHP >= 5.2.0 上运行,比如this
  • @Glavić:哎呀。我已经更新了答案。谢谢你:)
【解决方案2】:

您可以为此使用strtotime('+1 week', $unixTimestamp)

<?php
    $startDate = '2013-11-07';
    $endDate = '2013-12-31';

    $startDateUnix = strtotime($startDate);
    $endDateUnix = strtotime($endDate);

    $dates = array();

    while ($startDateUnix < $endDateUnix) {
        $dates[] = date('Y-m-d', $startDateUnix);
        $startDateUnix = strtotime('+1 week', $startDateUnix);
    }

    print_r($dates);
?>

输出:

Array
(
    [0] => 2013-11-07
    [1] => 2013-11-14
    [2] => 2013-11-21
    [3] => 2013-11-28
    [4] => 2013-12-05
    [5] => 2013-12-12
    [6] => 2013-12-19
    [7] => 2013-12-26
)

DEMO

(以任何你想要的方式格式化date()调用以获得你想要的格式)。

【讨论】:

    【解决方案3】:

    strtotime 满足您的需求

     $nextWeek = strtotime('08/11/2013 + 1 week'); 
    

    如果需要 8 次,请循环 8 次。您可以使用$start$numWeek 创建一个函数以返回具有$numWeeks+1 值的数组(添加的开头)

    function createDateList($start, $numWeeks){
        $dates = array($start);// add first date
        // create a loop with $numWeeks illiterations:
        for($i=1;$<=$numWeeks; $i++){
            // Add the weeks, take the first value and add $i weeks to it
            $time = strtotime($dates[0].' + '.$i.' week'); // get epoch value
            $dates[] = date("d/M/Y", $time); // set to prefered date format
    
        }
        return $dates;
    }
    

    【讨论】:

      【解决方案4】:

      strtotime() 函数可以在这里工作吗?

      $nextweek = strtotime('thursday next week');
      $date = date('d/m/Y', $nextweek);
      

      创建一个包含今天(或本周四)和下一个 4 的 5 元素数组:

      for ($a = 0; $a < 5; $a++)
      {
        $thur = date('d/m/Y', strtotime("thursday this week + $a weeks"));
        $dates[] = $thur;
      }
      

      【讨论】:

      • 但是你是如何连续数周这样做的?
      猜你喜欢
      • 1970-01-01
      • 2017-09-17
      • 2010-11-19
      • 1970-01-01
      • 1970-01-01
      • 2017-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多