2017-07-26 64 views

回答

0

試試這個:

$start = 22; 
$end = 08; 
$now = Carbon::now('UTC'); 

if($start < $now->hour $$ $now->hour < $end){ 
    // Do something 
} 
+0

這不是有效的php – Chris

+0

是的,你說得對 –

0
<?php 
$now = date("H"); 

if ($now < "20") { 
    echo "Have a good day!"; 
} 
+0

沒有解決日期範圍問題(即$ now> $ start && $ now <$ end是不夠的) – Chris

+0

這並沒有提供問題的答案。一旦你有足夠的[聲譽](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-c​​an- I-DO-代替)。 - [發表評論](/ review/low-quality-posts/16849419) – MikeT

0
$start = '22:00:00'; 
$end = '08:00:00'; 
$now = Carbon::now('UTC'); 
$time = $now->format('H:i:s'); 

if ($time >= $start || $time <= $end) { 
    ... 
} 

應該這樣做,但不採取日期考慮

0

請嘗試以下代碼,

$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'; 
} 
+0

沒有解決日期範圍問題(即$ now> $ start && $現在<$ end不夠用) – Chris

+0

其實你想要的時間範圍?不是嗎?請再次嘗試回答代碼。如果它不是解決描述我。 –

0

試試這個:

$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進行比較的更多信息。

相關問題