$start = '22:00:00';
$end = '08:00:00';
$now = Carbon::now('UTC');
如何檢查$ now的時間是否在時間範圍內?php碳檢查是否現在在兩次之間(10 pm-8am)
$start = '22:00:00';
$end = '08:00:00';
$now = Carbon::now('UTC');
如何檢查$ now的時間是否在時間範圍內?php碳檢查是否現在在兩次之間(10 pm-8am)
試試這個:
$start = 22;
$end = 08;
$now = Carbon::now('UTC');
if($start < $now->hour $$ $now->hour < $end){
// Do something
}
這不是有效的php – Chris
是的,你說得對 –
<?php
$now = date("H");
if ($now < "20") {
echo "Have a good day!";
}
沒有解決日期範圍問題(即$ now> $ start && $ now <$ end是不夠的) – Chris
這並沒有提供問題的答案。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can- I-DO-代替)。 - [發表評論](/ review/low-quality-posts/16849419) – MikeT
$start = '22:00:00';
$end = '08:00:00';
$now = Carbon::now('UTC');
$time = $now->format('H:i:s');
if ($time >= $start || $time <= $end) {
...
}
應該這樣做,但不採取日期考慮
請嘗試以下代碼,
$start = '22:00:00';
$end = '08:00:00';
$now = Carbon::now('UTC');
$nowTime = $now->hour.':'.$now->minute.':'.$now->second;
if(strtotime($nowTime) > strtotime($start) && strtotime($nowTime) < strtotime($end)) {
echo 'YES';
} else {
echo 'NO';
}
沒有解決日期範圍問題(即$ now> $ start && $現在<$ end不夠用) – Chris
其實你想要的時間範圍?不是嗎?請再次嘗試回答代碼。如果它不是解決描述我。 –
試試這個:
個$time = Carbon::now();
$morning = Carbon::create($time->year, $time->month, $time->day, 8, 0, 0); //set time to 08:00
$evening = Carbon::create($time->year, $time->month, $time->day, 18, 0, 0); //set time to 18:00
if($time->between($morning, $evening, true)) {
//true
} else {
//false
}
的true
在$time->between($morning, $evening, true)
檢查$time
是否在邊界之間或者等於或不,如果你把它設置爲false
它只是檢查,如果它是兩次,但不等於之間。
其實,你可以離開true
,因爲它是默認設置並且不需要。
檢查here以瞭解有關如何將日期和時間與Carbon進行比較的更多信息。
如果$ start和$ end在同一天內,$ now不在該範圍內。 –