日期年月数组

Array of date years and months

我在网上搜索过类似的问题,但不是我想要的。我本质上是在将 2 个数组传递给 API 之后,因此它们需要采用正确的格式。我传递的第一个数组是这样的

$dateArray = array(
    '2015' => '2015-01-01T00:00:00',
    '2016' => '2016-01-01T00:00:00',
    '2017' => '2017-01-01T00:00:00',
    '2018' => '2018-01-01T00:00:00'
);

目前这是硬编码的,但我想让它动态化,所以我不必更新它。我所知道的是开始年份是 2015 年,结束年份永远是当年。目前我正在尝试这个

$years = array_combine(range(date("Y"), 2015), range(date("Y"), 2015));

这给了我类似下面的东西

array:4 [
  2018 => 2018
  2017 => 2017
  2016 => 2016
  2015 => 2015
]

现在这几乎是正确的,年份的顺序无关紧要,但我不确定如何最好地获得数组右侧的这部分 2015-01-01T00:00:00

我之后的第二个数组非常相似。开始始终是 01/2015,当前始终是当前的月份和年份。硬编码版本如下所示

$dateArray = array(
    '2015-01' => '2015-01-01T00:00:00',
    '2015-02' => '2015-02-01T00:00:00',
    '2015-03' => '2015-03-01T00:00:00',
    '2015-04' => '2015-04-01T00:00:00',
    ...
);

这个我不太清楚,我试过了

$years = array_combine(range(date("Y-m"), 2015-01), range(date("Y-m"), 2015-01));

但这似乎只输出了岁月。我将如何动态生成这个数组?

如果它总是从 2015 年开始并且总是 1 月 1 日,如图所示,一个简单的循环应该可以工作。

for ($Y = 2015; $Y <= date('Y'); $Y++) {
    $yearArray[$Y] = $Y . '-01-01T00:00:00';
    $months = $Y < date('Y') ? 12 : date('n');

    for ($m = 1; $m <= $months; $m++) {
        $arrayKey = sprintf('%d-%02d', $Y, $m);
        $monthArray[$arrayKey] = $arrayKey . '-01T00:00:00';
    }
}

我建议您使用 DateTime::createFromFormat() 方法使用 DateTime class。

<?php

$startYear = 2010;
$array = [];

for ($x = $startYear; $x <= date('Y'); $x ++) {
    $date = DateTime::createFromFormat('Y-m-d\TH:i:s', $x . '-01-01T00:00:00');
    $array[$x] = $date->format('Y-m-d\TH:i:s');
}

var_dump($array);

这将输出:

array(9) { 
[2010]=> string(19) "2010-01-01T00:00:00" 
[2011]=> string(19) "2011-01-01T00:00:00" 
[2012]=> string(19) "2012-01-01T00:00:00" 
[2013]=> string(19) "2013-01-01T00:00:00" 
[2014]=> string(19) "2014-01-01T00:00:00" 
[2015]=> string(19) "2015-01-01T00:00:00" 
[2016]=> string(19) "2016-01-01T00:00:00" 
[2017]=> string(19) "2017-01-01T00:00:00" 
[2018]=> string(19) "2018-01-01T00:00:00" 
}

您可以点击此处亲自查看 https://3v4l.org/9gVRg

理论上你可以用字符串来做,但是花时间玩 DateTime class 是非常值得的。 http://php.net/manual/en/class.datetime.php,甚至可能只是存储对象而不是数组中的字符串。

这是获取第二个数组的方法,我以稍微不同的方式完成了它,只是为了向您展示另一种完成方式。

<?php

$array2 = [];
$year = 2010;
for ($x = 1; $x <= 12; $x ++) {
    $array2[$year.'-'.$x] = (new DateTime($year.'-'.$x.'-01T00:00:00'))->format('Y-m-d\TH:i:s');
}

var_dump($array2);

再一次,这里的代码 https://3v4l.org/C5hQ7

<?php
$keyArr = range(date("Y"), 2015);
$valArr = array_map(function($a){return $a.'-01-01-T00:00:00';},$keyArr);
$years = array_combine($keyArr,$valArr);
?>

这将为您提供所需的输出。

如果您出于某种原因不喜欢代码中的循环,您可以 运行 在 $years 上这样做。

array_walk($years, function(&$val) { $val = sprintf('%d-01-01T00:00:00', $val); });

应该这样做:

<?php
// Store your years here
$years = array();

// Store the monthly dates here
$months = array();

// This is the starting point
$start = new DateTime( '2015-01-01 00:00:00' );

// while we are working with a date which is less than the current time then keep building the array
while( $start->getTimeStamp() <= time() )
{
    // Populate a year
    $years[ $start->format( 'Y' ) ] = $start->format( 'Y' ).'-01-01T00:00:00';

    // Populate a month
    $months[ $start->format( 'Y-m' ) ] = $start->format( 'Y-m' ).'-01T00:00:00';

    // Always add a month since that is the smallest interval we need to store
    $start->modify( 'first day of +1 month' );
}

print_r( $years );
print_r( $months );

输出:

Array
(
    [2015] => 2015-01-01T00:00:00
    [2016] => 2016-01-01T00:00:00
    [2017] => 2017-01-01T00:00:00
    [2018] => 2018-01-01T00:00:00
)
Array
(
    [2015-01] => 2015-01-01T00:00:00
    [2015-02] => 2015-02-01T00:00:00
    [2015-03] => 2015-03-01T00:00:00
    [2015-04] => 2015-04-01T00:00:00
    [2015-05] => 2015-05-01T00:00:00
    [2015-06] => 2015-06-01T00:00:00
    [2015-07] => 2015-07-01T00:00:00
    [2015-08] => 2015-08-01T00:00:00
    [2015-09] => 2015-09-01T00:00:00
    [2015-10] => 2015-10-01T00:00:00
    [2015-11] => 2015-11-01T00:00:00
    [2015-12] => 2015-12-01T00:00:00
    [2016-01] => 2016-01-01T00:00:00
    [2016-02] => 2016-02-01T00:00:00
    [2016-03] => 2016-03-01T00:00:00
    [2016-04] => 2016-04-01T00:00:00
    [2016-05] => 2016-05-01T00:00:00
    [2016-06] => 2016-06-01T00:00:00
    [2016-07] => 2016-07-01T00:00:00
    [2016-08] => 2016-08-01T00:00:00
    [2016-09] => 2016-09-01T00:00:00
    [2016-10] => 2016-10-01T00:00:00
    [2016-11] => 2016-11-01T00:00:00
    [2016-12] => 2016-12-01T00:00:00
    [2017-01] => 2017-01-01T00:00:00
    [2017-02] => 2017-02-01T00:00:00
    [2017-03] => 2017-03-01T00:00:00
    [2017-04] => 2017-04-01T00:00:00
    [2017-05] => 2017-05-01T00:00:00
    [2017-06] => 2017-06-01T00:00:00
    [2017-07] => 2017-07-01T00:00:00
    [2017-08] => 2017-08-01T00:00:00
    [2017-09] => 2017-09-01T00:00:00
    [2017-10] => 2017-10-01T00:00:00
    [2017-11] => 2017-11-01T00:00:00
    [2017-12] => 2017-12-01T00:00:00
    [2018-01] => 2018-01-01T00:00:00
    [2018-02] => 2018-02-01T00:00:00
    [2018-03] => 2018-03-01T00:00:00
    [2018-04] => 2018-04-01T00:00:00
    [2018-05] => 2018-05-01T00:00:00
    [2018-06] => 2018-06-01T00:00:00
)

@MonkeyZeus 的略微修改版本,使用 DateInterval class:

$start = new DateTime( '2015-01-01' );
// without `noon` it won't work if now were the first of a month
$end = new DateTime( 'today noon' ); 
$interval = new DateInterval( 'P1M' );

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

$data = array();
foreach ( $period as $date ) {
    // it would make more sense to use DATE_W3C as format ...
    $data[ $date->format( 'Y-m' ) ] = $date->format( 'Y-m-d\TH:i:s' );
}