首页 > 解决方案 > 如何在laravel中接收两个日期之间的所有天数

问题描述

考虑到我认为我有 2 个如下日期:

{{$property->start_date }} //which is for example 3/20/219

和另一个日期

{{$property->end_date }} //which is for example 3/28/219

现在我想一些如何将这 8 天的差异打印为 8 col-lg-1 一些如何像下面的代码

@while($property->start_date  <= $property->end_date)

//here i want to print dates in col-lg-1 i dont know how to access the day and if the while loop is working right or no

考虑到在我看来我正在这样做并且在我看来这样做是否合理,我如何在刀片中实现这一点。

标签: phplaravel

解决方案


您可以使用CarbonPeriodCarbon

像这样的东西

$ranges = CarbonPeriod::create('2019-03-01', '2019-03-31');

要打印每个日期,您可以使用循环

foreach ($ranges as $date) {
    echo $date->format('Y-m-d');
}

或者您也可以将其转换为数组

$dates = $ranges->toArray();

推荐阅读