php 数组中包含一个月的 DateInterval
DateInterval with one month in php array
如何使用 DateInterval
.
获得 php 中的月份间隔
如果我选择 2015 年 8 月 5 日 作为开始日期,17 十月 2015 作为结束日期,那么我的数组输出应该分为3 个数组:
- 第一个数组 2015 年 8 月 5 日 - 2015 年 8 月 31 日
- 2015 年 9 月 1 日 - 2015 年 9 月 30 日的第 2 个数组
- 2015 年 10 月 1 日 - 2015 年 10 月 17 日的第 3 个数组
我们可以通过phpDateInterval
函数来实现这个东西?
如果您熟悉 PHP 的一些有用的内置 Date/Time 类,这将非常简单。
您可以使用 DateTime
, DateInterval
and DatePeriod
来实现您的结果。例如:
<?php
// create start/end dates and interval
$start = new DateTime('05 Aug 2015');
$end = new DateTime('17 Oct 2015');
$interval = new DateInterval('P1D');
// DatePeriod excludes the last day
// so bump the end date by one
$end->modify('+1 day');
// create a DatePeriod for each day in range
$period = new DatePeriod($start, $interval, $end);
$months = [];
foreach ($period as $date) {
$months[$date->format('F')][] = $date->format('m/d/Y');
}
print_r($months);
产量:
Array
(
[August] => Array
(
[0] => 08/05/2015
[1] => 08/06/2015
[2] => 08/07/2015
[3] => 08/08/2015
[4] => 08/09/2015
[5] => 08/10/2015
[6] => 08/11/2015
[7] => 08/12/2015
[8] => 08/13/2015
[9] => 08/14/2015
[10] => 08/15/2015
[11] => 08/16/2015
[12] => 08/17/2015
[13] => 08/18/2015
[14] => 08/19/2015
[15] => 08/20/2015
[16] => 08/21/2015
[17] => 08/22/2015
[18] => 08/23/2015
[19] => 08/24/2015
[20] => 08/25/2015
[21] => 08/26/2015
[22] => 08/27/2015
[23] => 08/28/2015
[24] => 08/29/2015
[25] => 08/30/2015
[26] => 08/31/2015
)
[September] => Array
(
[0] => 09/01/2015
[1] => 09/02/2015
[2] => 09/03/2015
[3] => 09/04/2015
[4] => 09/05/2015
[5] => 09/06/2015
[6] => 09/07/2015
[7] => 09/08/2015
[8] => 09/09/2015
[9] => 09/10/2015
[10] => 09/11/2015
[11] => 09/12/2015
[12] => 09/13/2015
[13] => 09/14/2015
[14] => 09/15/2015
[15] => 09/16/2015
[16] => 09/17/2015
[17] => 09/18/2015
[18] => 09/19/2015
[19] => 09/20/2015
[20] => 09/21/2015
[21] => 09/22/2015
[22] => 09/23/2015
[23] => 09/24/2015
[24] => 09/25/2015
[25] => 09/26/2015
[26] => 09/27/2015
[27] => 09/28/2015
[28] => 09/29/2015
[29] => 09/30/2015
)
[October] => Array
(
[0] => 10/01/2015
[1] => 10/02/2015
[2] => 10/03/2015
[3] => 10/04/2015
[4] => 10/05/2015
[5] => 10/06/2015
[6] => 10/07/2015
[7] => 10/08/2015
[8] => 10/09/2015
[9] => 10/10/2015
[10] => 10/11/2015
[11] => 10/12/2015
[12] => 10/13/2015
[13] => 10/14/2015
[14] => 10/15/2015
[15] => 10/16/2015
[16] => 10/17/2015
)
)
请注意:您指定要将结果分成三个单独的数组。我假设结果将是动态的——其中月数可能会有所不同——所以我认为你最好生成一个多维结果数组。
在上面的示例中,我使用了月份名称作为键。 $date->format('F')
returns一个full textual representation of a month, such as January or March.
如果你想获得 $months
数组中包含的月份列表,你可以这样做:
print_r(array_keys($months));
哪个 returns 键,应该给你足够的信息来访问你需要的数据:
Array
(
[0] => August
[1] => September
[2] => October
)
希望这对您有所帮助:)
您可以试试下面的代码。我不确定,但你可能会得到你想要的东西
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($d1, $interval, $d2);
foreach ($period as $dt) {
if($dt->format("Y-m") == date("Y-m", strtotime($date1))){
$date_array[$dt->format("Y-m")] = array(
'start_date' => $startDate,
'end_date' => $dt->format("Y-m-t"),
);
}else if($dt->format("Y-m") == date("Y-m", strtotime($date2))){
$date_array[$dt->format("Y-m")] = array(
'start_date' => $dt->format("Y-m-").'01',
'end_date' => $endDate,
);
}else{
$date_array[$dt->format("Y-m")] = array(
'start_date' => $dt->format("Y-m-").'01',
'end_date' => $dt->format("Y-m-t"),
);
}
}
foreach( $date_array as $d_key=>$da ){
/* your stuff */
}
如何使用 DateInterval
.
如果我选择 2015 年 8 月 5 日 作为开始日期,17 十月 2015 作为结束日期,那么我的数组输出应该分为3 个数组:
- 第一个数组 2015 年 8 月 5 日 - 2015 年 8 月 31 日
- 2015 年 9 月 1 日 - 2015 年 9 月 30 日的第 2 个数组
- 2015 年 10 月 1 日 - 2015 年 10 月 17 日的第 3 个数组
我们可以通过phpDateInterval
函数来实现这个东西?
如果您熟悉 PHP 的一些有用的内置 Date/Time 类,这将非常简单。
您可以使用 DateTime
, DateInterval
and DatePeriod
来实现您的结果。例如:
<?php
// create start/end dates and interval
$start = new DateTime('05 Aug 2015');
$end = new DateTime('17 Oct 2015');
$interval = new DateInterval('P1D');
// DatePeriod excludes the last day
// so bump the end date by one
$end->modify('+1 day');
// create a DatePeriod for each day in range
$period = new DatePeriod($start, $interval, $end);
$months = [];
foreach ($period as $date) {
$months[$date->format('F')][] = $date->format('m/d/Y');
}
print_r($months);
产量:
Array
(
[August] => Array
(
[0] => 08/05/2015
[1] => 08/06/2015
[2] => 08/07/2015
[3] => 08/08/2015
[4] => 08/09/2015
[5] => 08/10/2015
[6] => 08/11/2015
[7] => 08/12/2015
[8] => 08/13/2015
[9] => 08/14/2015
[10] => 08/15/2015
[11] => 08/16/2015
[12] => 08/17/2015
[13] => 08/18/2015
[14] => 08/19/2015
[15] => 08/20/2015
[16] => 08/21/2015
[17] => 08/22/2015
[18] => 08/23/2015
[19] => 08/24/2015
[20] => 08/25/2015
[21] => 08/26/2015
[22] => 08/27/2015
[23] => 08/28/2015
[24] => 08/29/2015
[25] => 08/30/2015
[26] => 08/31/2015
)
[September] => Array
(
[0] => 09/01/2015
[1] => 09/02/2015
[2] => 09/03/2015
[3] => 09/04/2015
[4] => 09/05/2015
[5] => 09/06/2015
[6] => 09/07/2015
[7] => 09/08/2015
[8] => 09/09/2015
[9] => 09/10/2015
[10] => 09/11/2015
[11] => 09/12/2015
[12] => 09/13/2015
[13] => 09/14/2015
[14] => 09/15/2015
[15] => 09/16/2015
[16] => 09/17/2015
[17] => 09/18/2015
[18] => 09/19/2015
[19] => 09/20/2015
[20] => 09/21/2015
[21] => 09/22/2015
[22] => 09/23/2015
[23] => 09/24/2015
[24] => 09/25/2015
[25] => 09/26/2015
[26] => 09/27/2015
[27] => 09/28/2015
[28] => 09/29/2015
[29] => 09/30/2015
)
[October] => Array
(
[0] => 10/01/2015
[1] => 10/02/2015
[2] => 10/03/2015
[3] => 10/04/2015
[4] => 10/05/2015
[5] => 10/06/2015
[6] => 10/07/2015
[7] => 10/08/2015
[8] => 10/09/2015
[9] => 10/10/2015
[10] => 10/11/2015
[11] => 10/12/2015
[12] => 10/13/2015
[13] => 10/14/2015
[14] => 10/15/2015
[15] => 10/16/2015
[16] => 10/17/2015
)
)
请注意:您指定要将结果分成三个单独的数组。我假设结果将是动态的——其中月数可能会有所不同——所以我认为你最好生成一个多维结果数组。
在上面的示例中,我使用了月份名称作为键。 $date->format('F')
returns一个full textual representation of a month, such as January or March.
如果你想获得 $months
数组中包含的月份列表,你可以这样做:
print_r(array_keys($months));
哪个 returns 键,应该给你足够的信息来访问你需要的数据:
Array
(
[0] => August
[1] => September
[2] => October
)
希望这对您有所帮助:)
您可以试试下面的代码。我不确定,但你可能会得到你想要的东西
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($d1, $interval, $d2);
foreach ($period as $dt) {
if($dt->format("Y-m") == date("Y-m", strtotime($date1))){
$date_array[$dt->format("Y-m")] = array(
'start_date' => $startDate,
'end_date' => $dt->format("Y-m-t"),
);
}else if($dt->format("Y-m") == date("Y-m", strtotime($date2))){
$date_array[$dt->format("Y-m")] = array(
'start_date' => $dt->format("Y-m-").'01',
'end_date' => $endDate,
);
}else{
$date_array[$dt->format("Y-m")] = array(
'start_date' => $dt->format("Y-m-").'01',
'end_date' => $dt->format("Y-m-t"),
);
}
}
foreach( $date_array as $d_key=>$da ){
/* your stuff */
}