如何在兩個隨機日期之間制作一個包含所有月份的時期?我試過:
$startDate = \Carbon\Carbon::parse('2021-11-17 23:59:59');
$endDate = \Carbon\Carbon::parse('2022-01-10 00:00:00');
$period = \Carbon\CarbonPeriod::create($startDate, '1 month', $endDate);
foreach($period as $month)
{
echo '<pre>'.$month->format('Y-m-d').'</pre>';
}
但它不包括一月。
我也嘗試使用 floor():
$startDate = \Carbon\Carbon::parse('2021-11-17 23:59:59');
$endDate = \Carbon\Carbon::parse('2022-01-31 00:00:00');
$period = \Carbon\CarbonPeriod::create($startDate, '1 month', $endDate)->floor();
foreach($period as $month)
{
echo '<pre>'.$month->format('Y-m-d').'</pre>';
}
但它包括甚至不需要的二月。
如何使用 CarbonPeriod 在兩個日期之間獲得純“單一”月份?
例如:起始日期:2021年11月17日23點59分59秒&END_DATE:2022年1月10日00:00:00 - > 11,12,01
也:起始日期:2021年11月17日23時59分五十九秒&END_DATE:00:00:00 2022年1月31日- > 11,12,01
謝謝你。
uj5u.com熱心網友回復:
使用startOfMonth()和 endOfMonth()如下。
$startDate = Carbon::parse('2021-11-17 23:59:59')->startOfMonth();
$endDate = Carbon::parse('2022-01-10 00:00:00')->endOfMonth();
foreach (CarbonPeriod::create( $startDate, '1 month', $endDate) as $month) {
echo $month->format('m') . PHP_EOL;
}
輸出將是:11 12 01
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/407947.html
標籤:
上一篇:向stdClass物件添加更多值
