$start = '22:00:00';
$end = '08:00:00';
$now = Carbon::now('UTC');
如何检查$ now的时间是否在时间范围内?
答案 0 :(得分:7)
有几种方法可以通过使用Carbon来实现。最简单的方法之一是使用createFromTimeString
和between
方法:
$now = Carbon::now();
$start = Carbon::createFromTimeString('22:00');
$end = Carbon::createFromTimeString('08:00')->addDay();
if ($now->between($start, $end)) {
// ¯\_(ツ)_/¯
}
答案 1 :(得分:4)
$start = '22:00:00';
$end = '08:00:00';
$now = Carbon::now('UTC');
$time = $now->format('H:i:s');
if ($time >= $start && $time <= $end) {
...
}
应该这样做,但不考虑日期
答案 2 :(得分:4)
试试这个:
COUNT(DISTINCT EmployeeID)
$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)) {
//current time is between morning and evening
} else {
//current time is earlier than morning or later than evening
}
中的true
会检查$time->between($morning, $evening, true)
是否介于$time
和$morning
之间。如果你写$evening
而不是检查它是否在两次之间但不包括。
实际上,您可以离开false
,因为它是默认设置的,不需要。
检查here,了解有关如何与碳比较日期和时间的更多信息。
答案 3 :(得分:0)
<?php
$now = date("H");
if ($now < "20") {
echo "Have a good day!";
}
答案 4 :(得分: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';
}
答案 5 :(得分:0)
$restrictStartTime = Carbon::createFromTime(22, 0, 0); //carbon inbuild function which will create todays date with the given time
$restrictEndTime = Carbon::createFromTime(8, 0, 0)->addDays(1); //this will create tomorrows date with the given time
$now = Carbon::now();
if($now->gt($restrictStartTime) && $now->lt($restrictEndTime)) {
.....
}
答案 6 :(得分:0)
克里斯想指出的是,如果结束时间超过午夜,那么您必须对此负责。
这不是最干净的方法,但这似乎是可行的方法。
private function isNowBetweenTimes($timezone, $startDateTime, $endDateTime) {
$curTimeLocal = Carbon::now($timezone);
$startTime = $curTimeLocal->copy();
$startTime->hour = $startDateTime->hour;
$startTime->minute = $startDateTime->minute;
$endTime = $curTimeLocal->copy();
$endTime->hour = $endDateTime->hour;
$endTime->minute = $endDateTime->minute;
if ($endTime->lessThan($startTime))
$endTime->addDay();
return ($curTimeLocal->isBetween($startTime, $endTime));
}
此示例仅关心小时和分钟,而不关心秒,但是您也可以轻松地复制它们。这样做的关键是先比较开始时间和结束时间,然后再将它们与当前时间进行比较;如果结束时间小于开始时间,则在结束时间前增加一天。
答案 7 :(得分:0)
您可以反向检查算法。
<?php
$pushChannel = "general";
$now = Carbon::now();
$start = Carbon::createFromTime(8, 0);
$end = Carbon::createFromTime(22, 0);
if (!$now->between($start, $end)) {
$pushChannel = "silent";
答案 8 :(得分:0)
对于支持所有开始和结束时间范围的完整解决方案,您可以使用按位XOR。
/*
* must using hours in 24 hours format e.g. set 0 for 12 pm, 6 for 6 am and 13 for 1 pm
*/
private $startTime = '0';
private $endTime = '6';
$currentHour = \Carbon\Carbon::now()->hour;
$start = $this->startTime > $this->endTime ? !($this->startTime <= $currentHour) : $this->startTime <= $currentHour;
$end = $currentHour < $this->endTime;
if (!($start ^ $end)) {
//Do stuff here if you want exactly between start and end time
}
答案 9 :(得分:-1)
试试这个:
foreach ($ids_array as $id) {
$O = new Object($id);
// stuff and things
}