在 php 中返回两个日期之间的所有日期:
使用DatePeriod和DateTime:
$begin = new DateTime($property->start_date); // your start date 2019-03-20
$begin = $begin->modify( '+1 day' );
$end = new DateTime($property->end_date); // your end date 2019-03-28
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
foreach ($daterange as $date) {
echo '<pre>'.$date->format('Y-m-d').'<pre>';
}
输出:
2019-03-21
2019-03-22
2019-03-23
2019-03-24
2019-03-25
2019-03-26
2019-03-27
使用strtotime:
// Declare two dates
$Date1 = $property->start_date; // 2019-03-20
$Date2 = $property->end_date; // 2019-03-28
// Declare an empty array
$array = array();
// Use strtotime function
$start = strtotime($Date1. '+1 day');
$end = strtotime($Date2. '-1 day');
// Use for loop to store dates into array
// 86400 sec = 24 hrs = 60*60*24 = 1 day
for ($currentDate = $start; $currentDate <= $end; $currentDate += (86400)) {
$Store = date('Y-m-d', $currentDate);
$array[] = $Store;
}
// Display the dates in array format
echo '<pre>';
print_r($array);
echo '<pre>';
输出:
Array
(
[0] => 2019-03-21
[1] => 2019-03-22
[2] => 2019-03-23
[3] => 2019-03-24
[4] => 2019-03-25
[5] => 2019-03-26
[6] => 2019-03-27
)
我希望它会有所帮助。