日期年和月的数组

时间:2018-06-13 12:22:33

标签: php date date-range

我在线搜索并发现了类似的问题,但不完全是我所追求的。我基本上在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));

但这似乎只能输出数年。我将如何动态生成此数组?

6 个答案:

答案 0 :(得分:3)

如果它总是从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';
    }
}

答案 1 :(得分:2)

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

<?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类。 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

答案 2 :(得分:2)

如果由于某种原因不喜欢代码中的循环,可以在$years上运行此功能。

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

答案 3 :(得分:1)

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

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

答案 4 :(得分:1)

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

$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' );
}

答案 5 :(得分:0)

这应该这样做:

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