碳检查时间
Carbon checking hours
我有功能
public static function isRestaurantOpen($RestaurantID)
{
$hours = settings_hours::all();
$restaurant = [];
foreach ($hours as $hour) {
if ($hour->place_id == $RestaurantID) {
$restaurant = $hour;
}
}
if(!$restaurant) return "Time not set";
$opening_hours = $restaurant->opening_hours;
$date = date_create_from_format('Y-m-d', date('Y-m-d', time())); //get date
$day = strtolower(date_format($date, 'l')); //get day of date
$now = Carbon::now()->addHours(2); //im added this because i have wrong time on the server
$start = Carbon::createFromTimeString($opening_hours[$day]['from']); //from hour
$end = Carbon::createFromTimeString($opening_hours[$day]['to']); //to hour
if ($now->between($start, $end)) {
return "Open";
} else {
return "Close";
}
}
但是如果以 24 小时格式比较时间,此功能将无法正常工作。
任何人都可以帮助我吗?谢谢:)
如果你想增加一个多小时,你应该使用 addHours 'with s' 而不是 addHour ...
$now = Carbon::now()->addHours(2);
$start = Carbon::parse($opening_hours[$day]['from']);
$end = Carbon::parse($opening_hours[$day]['to']);
现在这段代码应该可以工作了:
if ($now->between($start, $end)) {
return "Open";
} else {
return "Close";
}
好的,我在另一个问题中找到了函数,这个函数很好用
public static 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));
}
听起来您每天都定义了开放时间,这正是 Carbon 文档中提到的业务时间混合的目的:
https://github.com/kylekatarnls/business-time
这很可能适合您。
我有功能
public static function isRestaurantOpen($RestaurantID)
{
$hours = settings_hours::all();
$restaurant = [];
foreach ($hours as $hour) {
if ($hour->place_id == $RestaurantID) {
$restaurant = $hour;
}
}
if(!$restaurant) return "Time not set";
$opening_hours = $restaurant->opening_hours;
$date = date_create_from_format('Y-m-d', date('Y-m-d', time())); //get date
$day = strtolower(date_format($date, 'l')); //get day of date
$now = Carbon::now()->addHours(2); //im added this because i have wrong time on the server
$start = Carbon::createFromTimeString($opening_hours[$day]['from']); //from hour
$end = Carbon::createFromTimeString($opening_hours[$day]['to']); //to hour
if ($now->between($start, $end)) {
return "Open";
} else {
return "Close";
}
}
但是如果以 24 小时格式比较时间,此功能将无法正常工作。 任何人都可以帮助我吗?谢谢:)
如果你想增加一个多小时,你应该使用 addHours 'with s' 而不是 addHour ...
$now = Carbon::now()->addHours(2);
$start = Carbon::parse($opening_hours[$day]['from']);
$end = Carbon::parse($opening_hours[$day]['to']);
现在这段代码应该可以工作了:
if ($now->between($start, $end)) {
return "Open";
} else {
return "Close";
}
好的,我在另一个问题中找到了函数,这个函数很好用
public static 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));
}
听起来您每天都定义了开放时间,这正是 Carbon 文档中提到的业务时间混合的目的:
https://github.com/kylekatarnls/business-time
这很可能适合您。