我的商店目前营业吗?使用碳
Is my shop currently open? using Carbon
我正在尝试以下操作:
我有两个模型:Pub 和 Schedule,它们存储给定酒吧的不同时间表。由于这个原因,这两个模型之间的关系是 1 pub->N schedules:
酒吧:
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function pubSchedules()
{
return $this->hasMany(Schedule::class);
}
日程安排:
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function pub()
{
return $this->belongsTo(Pub::class);
}
table"schedules"有以下字段:
id|pub_id|week_day|opening_time|closing_time
例如,对于 pub wit id =303,我们有以下时间表
编号| pub_id|week_day|opening_time|closing_time
'9', '303', '5', '00:00:00', '02:30:00'
'10', '303', '5', '10:30:00', '13:30:00'
'11', '303', '5', '20:00:00', '23:59:00'
我正在尝试做一个函数来了解给定的酒吧当前是否开放,但由于我对 Carbon 了解不多,所以我有一些问题需要解决。
我想知道如何将当前时间与给定酒吧白天不同的开门和关门时间进行比较,并知道当前时间是否在该酒吧营业的时间间隔内.
我试过了(我想要功能 return 消息 "open/closed"):
(粗略版)
public function isPubCurrentlyOpen(Pub $pub)
{
$dayOfWeek = Carbon::now()->dayOfWeek;
$schedules = Schedule::where([
['pub_id', $pub->id ],
['week_day', $dayOfWeek],
])->get();
foreach($schedules as $schedule)
{
if(var_dump($dayOfWeek->between($schedule->opening_time, $schedule->closing_time))){
return "Open";
}
}
}
拜托,你能帮帮我吗?
谢谢!!
以下应该可以解决问题:
$isOpen =
// check if day of week matches
Carbon::now()->dayOfWeek === $schedule->week_day &&
// check if current time is greater than or equal than today's opening time
Carbon::now()->gte(Carbon::now()->setTimeFromTimeString($schedule->opening_time)) &&
// check if current time is less than or equal than today's closing time
Carbon::now()->lte(Carbon::now()->setTimeFromTimeString($schedule->closing_time));
更新: 正如下面 Thomas 所建议的,您可以使用 between 方法来简化代码:
$isOpen =
Carbon::now()->dayOfWeek === $schedule->week_day &&
Carbon::now()->between(
Carbon::now()->setTimeFromTimeString($schedule->opening_time),
Carbon::now()->setTimeFromTimeString($schedule->closing_time)
);
解决方案(感谢 jedrzej.kurylo 和 Thomas Moors):
public function isPubCurrentlyOpen(Pub $pub)
{
$dayOfWeek = Carbon::now()->dayOfWeek;
$schedules = Schedule::where([
['pub_id', $pub->id ],
['week_day', $dayOfWeek],
])->get();
foreach ($schedules as $schedule){
$isOpen[] =
Carbon::now()->between(
Carbon::now()->setTimeFromTimeString($schedule->opening_time),
Carbon::now()->setTimeFromTimeString($schedule->closing_time)
);
}
if(in_array(true, $isOpen)){
return "Pub Opened";
}
return "Pub Closed";
}
我正在尝试以下操作:
我有两个模型:Pub 和 Schedule,它们存储给定酒吧的不同时间表。由于这个原因,这两个模型之间的关系是 1 pub->N schedules:
酒吧:
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function pubSchedules()
{
return $this->hasMany(Schedule::class);
}
日程安排:
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function pub()
{
return $this->belongsTo(Pub::class);
}
table"schedules"有以下字段:
id|pub_id|week_day|opening_time|closing_time
例如,对于 pub wit id =303,我们有以下时间表
编号| pub_id|week_day|opening_time|closing_time '9', '303', '5', '00:00:00', '02:30:00' '10', '303', '5', '10:30:00', '13:30:00' '11', '303', '5', '20:00:00', '23:59:00'
我正在尝试做一个函数来了解给定的酒吧当前是否开放,但由于我对 Carbon 了解不多,所以我有一些问题需要解决。
我想知道如何将当前时间与给定酒吧白天不同的开门和关门时间进行比较,并知道当前时间是否在该酒吧营业的时间间隔内.
我试过了(我想要功能 return 消息 "open/closed"):
(粗略版)
public function isPubCurrentlyOpen(Pub $pub)
{
$dayOfWeek = Carbon::now()->dayOfWeek;
$schedules = Schedule::where([
['pub_id', $pub->id ],
['week_day', $dayOfWeek],
])->get();
foreach($schedules as $schedule)
{
if(var_dump($dayOfWeek->between($schedule->opening_time, $schedule->closing_time))){
return "Open";
}
}
}
拜托,你能帮帮我吗? 谢谢!!
以下应该可以解决问题:
$isOpen =
// check if day of week matches
Carbon::now()->dayOfWeek === $schedule->week_day &&
// check if current time is greater than or equal than today's opening time
Carbon::now()->gte(Carbon::now()->setTimeFromTimeString($schedule->opening_time)) &&
// check if current time is less than or equal than today's closing time
Carbon::now()->lte(Carbon::now()->setTimeFromTimeString($schedule->closing_time));
更新: 正如下面 Thomas 所建议的,您可以使用 between 方法来简化代码:
$isOpen =
Carbon::now()->dayOfWeek === $schedule->week_day &&
Carbon::now()->between(
Carbon::now()->setTimeFromTimeString($schedule->opening_time),
Carbon::now()->setTimeFromTimeString($schedule->closing_time)
);
解决方案(感谢 jedrzej.kurylo 和 Thomas Moors):
public function isPubCurrentlyOpen(Pub $pub)
{
$dayOfWeek = Carbon::now()->dayOfWeek;
$schedules = Schedule::where([
['pub_id', $pub->id ],
['week_day', $dayOfWeek],
])->get();
foreach ($schedules as $schedule){
$isOpen[] =
Carbon::now()->between(
Carbon::now()->setTimeFromTimeString($schedule->opening_time),
Carbon::now()->setTimeFromTimeString($schedule->closing_time)
);
}
if(in_array(true, $isOpen)){
return "Pub Opened";
}
return "Pub Closed";
}