![](/img/trans.png)
[英]PHP Getting array with years, months and dates given a start and end date
[英]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类。
<?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
如果您由于某种原因不喜欢代码中的循环,可以在$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);
?>
这将为您提供所需的输出。
@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' );
}
应该这样做:
<?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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.