我有兩個日期:$from = 2022-11-01 $to = 2022-11-05
現在,我想創建一個這樣的陣列:$date = ['2022-11-01','2022-11-02','2022-11-03','2022-11-04','2022-11 -05']
現在檢查 $date 陣列中是否存在“2022-11-03”。
已經使用:
$date = Carbon\CarbonPeriod::create($from, $to);
if(in_array('2022-11-03', $date)){
echo "Got it";
}
#但還是不行
uj5u.com熱心網友回復:
您可以只檢查 CarbonPeriod 是否包含您檢查的日期:
$from = '2022-11-01';
$to = '2022-11-05';
$date = Carbon\CarbonPeriod::create($from, $to);
if ($date->contains('2022-11-03')) {
echo 'Got it';
}
uj5u.com熱心網友回復:
有人要求使用 CarbonPeriod 解決方案。但是,如果您只想檢查日期是否介于 $from 和 $to 之間,那么 between 是更好的選擇。
$from = "2022-11-01";
$to = "2022-11-05";
$check = "2022-11-03";
if(Carbon::parse($check)->between($from,$to)){
echo $check.' is between '.$from.' and '.$to;
} else {
echo $check.' is not between '.$from.' and '.$to;
}
試試碳
如果資料都是yyyy-mm-dd格式,簡單的字串比較就可以了。
$from = "2022-11-01";
$to = "2022-11-05";
$check = "2022-11-03";
if($from <= $check AND $check <= $to) {
echo $check.' is between '.$from.' and '.$to;
} else {
echo $check.' is not between '.$from.' and '.$to;
}
演示:https ://3v4l.org/A1IBh
uj5u.com熱心網友回復:
根據問題,您可能沒有使用parse方法來決議日期,所以這可能是問題所在。
$from = "2022-11-01";
$to = "2022-11-05";
$dates = CarbonPeriod::create(
Carbon::parse($from),
Carbon::parse($to),
);
$dateArray = [];
foreach ($dates as $date) {
$dateArray[] = $date->toDateString();
}
在這里,$dateArray為您提供日期陣列。
此外,如果部分適用于上述解決方案:
if(in_array('2022-11-03', $date)){
echo "Got it";
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/535704.html
