如何在PHP中获取两个日期之间的所有日期?最好使用Carbon处理日期。
$from = Carbon::now();
$to = Carbon::createFromDate(2017, 5, 21);
我想要获取这两个日期之间的所有日期,但是怎么做呢?我只能找到使用strtotime函数的解决方案。
如何在PHP中获取两个日期之间的所有日期?最好使用Carbon处理日期。
$from = Carbon::now();
$to = Carbon::createFromDate(2017, 5, 21);
我想要获取这两个日期之间的所有日期,但是怎么做呢?我只能找到使用strtotime函数的解决方案。
截至Carbon 1.29,现在可以做到:
$period = CarbonPeriod::create('2018-06-14', '2018-06-20');
// Iterate over the period
foreach ($period as $date) {
echo $date->format('Y-m-d');
}
// Convert the period to an array of dates
$dates = $period->toArray();
详见文档以获取更多细节:https://carbon.nesbot.com/docs/#api-period。
这是我如何使用Carbon
完成的
private function generateDateRange(Carbon $start_date, Carbon $end_date)
{
$dates = [];
for($date = $start_date->copy(); $date->lte($end_date); $date->addDay()) {
$dates[] = $date->format('Y-m-d');
}
return $dates;
}
$start_date
等于$end_date
的问题吗?
如果您想在此函数调用后继续使用原始的$start_date
,则应将原始的$start_date
副本传递给此函数或在for
循环的定义中设置$date = $start_date -> copy()
,我更喜欢后者。 - molerat由于Carbon是PHP内置DateTime的扩展,您应该能够像使用DateTime对象一样使用DatePeriod和DateInterval。
$interval = new DateInterval('P1D');
$to->add($interval);
$daterange = new DatePeriod($from, $interval ,$to);
foreach($daterange as $date){
echo $date->format("Ymd"), PHP_EOL;
}
编辑
如果您需要包括周期的最后日期,则需要稍微修改它,并在生成DatePeriod之前调整$to
。
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($from, $interval ,$to);
foreach($daterange as $date){
echo $date->format("Ymd"), PHP_EOL;
}
基于 Mark Baker 的回答,我编写了这个函数:
/**
* Compute a range between two dates, and generate
* a plain array of Carbon objects of each day in it.
*
* @param \Carbon\Carbon $from
* @param \Carbon\Carbon $to
* @param bool $inclusive
* @return array|null
*
* @author Tristan Jahier
*/
function date_range(Carbon\Carbon $from, Carbon\Carbon $to, $inclusive = true)
{
if ($from->gt($to)) {
return null;
}
// Clone the date objects to avoid issues, then reset their time
$from = $from->copy()->startOfDay();
$to = $to->copy()->startOfDay();
// Include the end date in the range
if ($inclusive) {
$to->addDay();
}
$step = Carbon\CarbonInterval::day();
$period = new DatePeriod($from, $step, $to);
// Convert the DatePeriod into a plain array of Carbon objects
$range = [];
foreach ($period as $day) {
$range[] = new Carbon\Carbon($day);
}
return ! empty($range) ? $range : null;
}
使用方法:
>>> date_range(Carbon::parse('2016-07-21'), Carbon::parse('2016-07-23'));
=> [
Carbon\Carbon {#760
+"date": "2016-07-21 00:00:00.000000",
+"timezone_type": 3,
+"timezone": "UTC",
},
Carbon\Carbon {#759
+"date": "2016-07-22 00:00:00.000000",
+"timezone_type": 3,
+"timezone": "UTC",
},
Carbon\Carbon {#761
+"date": "2016-07-23 00:00:00.000000",
+"timezone_type": 3,
+"timezone": "UTC",
},
]
你还可以将一个布尔值(false
)作为第三个参数传递,以排除结束日期。
new DatePeriod($startDate, new DateInterval('P1D'), $endDate)
请记住,DatePeriod
是一个迭代器,如果你想要一个实际的数组:
iterator_to_array(new DatePeriod($startDate, new DateInterval('P1D'), $endDate))
如果您正在使用Laravel,您可以创建一个Carbon宏:
Carbon::macro('range', function ($start, $end) {
return new Collection(new DatePeriod($start, new DateInterval('P1D'), $end));
});
foreach (Carbon::range($start, $end) as $date) {
// ...
}
这是我所拥有的:
private function getDatesFromRange($date_time_from, $date_time_to)
{
// cut hours, because not getting last day when hours of time to is less than hours of time_from
// see while loop
$start = Carbon::createFromFormat('Y-m-d', substr($date_time_from, 0, 10));
$end = Carbon::createFromFormat('Y-m-d', substr($date_time_to, 0, 10));
$dates = [];
while ($start->lte($end)) {
$dates[] = $start->copy()->format('Y-m-d');
$start->addDay();
}
return $dates;
}
例子:
$this->getDatesFromRange('2015-03-15 10:10:10', '2015-03-19 09:10:10');
$start = Carbon::createFromDate(2017, 5, 21);
$end = Carbon::now();
while($start < $end){
echo $start->format("M");
$start->addMonth();
}
$start = Carbon::today()->startOfWeek();
$end = Carbon::today()->endOfWeek();
$stack = [];
$date = $start;
while ($date <= $end) {
if (! $date->isWeekend()) {
$stack[] = $date->copy();
}
$date->addDays(1);
}
return $stack;
如果您使用此函数,我认为会更好。
public static function check_continuous_dates(array $dates): bool
{
$startDate = Carbon::create($dates[0]);
$endDate = Carbon::create(end($dates));
if ($endDate->diffInDays($startDate)+1 == count($dates)) {
return true;
}
return false;
}
例子:
<?php
$from = "2023-05-02";
$to = "2023-05-31";
$betweenDate = CarbonPeriod::create($from, $to);
$dates = [];
foreach ($betweenDate as $date) {
$dates[] =$date->format('Y-m-d');
}
dd($dates);
?>
输出:
0 => "2023-05-02" 1 => "2023-05-03" 2 => "2023-05-04" 3 => "2023-05-05" 4 => "2023-05-06" 5 => "2023-05-07" 6 => "2023-05-08" 7 => "2023-05-09" 8 => "2023-05-10" 9 => "2023-05-11" 10 => "2023-05-12" 11 => "2023-05-13" 12 => "2023-05-14" 13 => "2023-05-15" 14 => "2023-05-16" 15 => "2023-05-17" 16 => "2023-05-18" 17 => "2023-05-19" 18 => "2023-05-20" 19 => "2023-05-21" 20 => "2023-05-22" 21 => "2023-05-23" 22 => "2023-05-24" 23 => "2023-05-25" 24 => "2023-05-26" 25 => "2023-05-27" 26 => "2023-05-28" 27 => "2023-05-29" 28 => "2023-05-30" 29 => "2023-05-31"
$date->toDateString()
以Y-m-d
格式获取日期。文档。 - ibnɘꟻ->format()
方法可以让你在配置文件中全局存储字符串格式。因此,每当需要在所有地方更改格式时,你不需要到处修改代码。 - Dendi Handian