简体   繁体   中英

Array of date years and months

I have searched online and found similar problems, but not exactly what I am after. I am essentially after 2 arrays to pass to an API, so they need to be in the correct format. The first array I pass is like so

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

At the moment this is hardcoded, but I wanted to make it dynamic so I didn't have to update it. What I know is that the start year is 2015, and the end year will always be the current year. At the moment I am trying this

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

And that gives me something like the following

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

Now that is nearly correct, the order of the years does not matter, but I am not sure how to best get this part 2015-01-01T00:00:00 on the right-hand side of the array?

The second array I am after is pretty similar. The start is always 01/2015, and the current will always be the current month and year. The hardcoded version looks something like the following

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

This one I am not sure about, I have tried

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

But that seems to only output the years still. How would I dynamically generate this array?

If it will always start with 2015 and always January 1st as shown a simple loop should work.

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

I recommend you use the DateTime class using the DateTime::createFromFormat() method.

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

This will output:

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" 
}

Which you can see for yourself by clicking here https://3v4l.org/9gVRg

Theoretically you could just do it in strings, but it is well worth your time playing with the DateTime class. http://php.net/manual/en/class.datetime.php , possibly even just storing the object instead of the string in your array.

Here's how to get your second array, which I have done in a slightly different way just to show you another way it can be done.

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

And again, the code here https://3v4l.org/C5hQ7

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

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

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

This will give you your desired output.

A slightly modified version of @MonkeyZeus, using the 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' );
}

This should do it:

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

Output:

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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM