每年生成一个每周日期

时间:2013-10-16 20:54:15

标签: php arrays date

基本上我想要的是像这样的数组,像

这样的数组
Array
(
    [20131020] => October 20 2013
    [20131027] => October 27 2013
    [20131103] => November 3 2013
    [20131110] => November 10 2013
)

其中索引是日期yyyy-mm-dd格式,每个索引的对应值是上面显示的等效m-dd-yyyy。它是否可以在PHP中生成,以便我可以生成每年和未来几年的每周日期?还有另一个看起来像这样的数组,

Array
(
    [20131020] => October 20 2013
        array(
            [20131020] = October 20 2013,
        [20131021] = October 21 2013,
        [20131022] = October 22 2013,
        [20131023] = October 23 2013,
        [20131024] = October 24 2013,
        [20131025] = October 25 2013,
        [20131026] = October 26 2013,
    )
    [20131027] => October 27 2013
        array(
        [20131020] = October 27 2013,
        [20131021] = October 28 2013,
        [20131022] = October 29 2013,
        [20131023] = October 30 2013,
        [20131024] = October 31 2013,
        [20131101] = November 01 2013,
        [20131102] = November 02 2013,
    )
)

我找不到任何类似的查询,所以我做了一个。任何帮助表示赞赏。感谢。

2 个答案:

答案 0 :(得分:1)

这应该让你开始:

$date     = new DateTime('2013-10-20');
$interval = new DateInterval('P1W');
$end      = new DateTime('2015-10-30');
$array    = array();

while($date < $end){
    $array[$date->format('Ymd')] = $date->format('F j, Y');
    $date->add($interval);
}

print_r($array);

答案 1 :(得分:0)

从日期开始,添加7天,直到您想要停止。

例如($dates将包括今天剩余时间之后的所有几周):

$theDate = time();
$dates = array();

while (date('Y', $theDate) == '2013') {
    $dates[] = $theDate;
    $theDate = strtotime('+7 days', $theDate);
}