PHP Get recurring week from specific start date

2020-05-03 11:27发布

I'm looking into trying to set up and array that will look something like this:

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

I'm willing to use this, but as I want this to reoccur again next year I'd prefer to have PHP do this and enter it into an array for me. I know how to limit it to a specific amount that I want, but I don't know how to add a week onto the current date or specific date if I wanted to start 08/11/2013 for example.

I've had a quick look and I can't seem to find anything that does this.

I just need a script to add a week to the current date, at the moment this is every Thursday, and then add that to the array.

My only problem is I'm not sure how to specify a date, and then add a week every time. I assume a for loop would be best here.

标签: php arrays date
4条回答
ら.Afraid
2楼-- · 2020-05-03 12:09

strtotime does what you need

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

If you need that 8 times, loop it 8 times. You can make a function with $start and $numWeek to return an array with $numWeeks+1 values (the start added)

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;
}
查看更多
不美不萌又怎样
3楼-- · 2020-05-03 12:12

Use DateTime class. DateInterval and DatePeriod classes were introduced in PHP 5.3.0, so the below solution works for only 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!

As Glavic notes in the comments below, this can also be done in previous versions of PHP using the modify() method:

$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.

查看更多
▲ chillily
4楼-- · 2020-05-03 12:16

You can use strtotime('+1 week', $unixTimestamp) for this:

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

Outputs:

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

(format the date() call in any way you want to get the format you want).

查看更多
▲ chillily
5楼-- · 2020-05-03 12:23

would the strtotime() function work here?

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

To create a 5 element array containing today (or this thursday) and the next 4:

for ($a = 0; $a < 5; $a++)
{
  $thur = date('d/m/Y', strtotime("thursday this week + $a weeks"));
  $dates[] = $thur;
}
查看更多
登录 后发表回答