0

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

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,
    )
)

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

4

2 回答 2

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);
于 2013-10-16T21:06:08.960 回答
0

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

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

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

while (date('Y', $theDate) == '2013') {
    $dates[] = $theDate;
    $theDate = strtotime('+7 days', $theDate);
}
于 2013-10-16T21:01:10.033 回答