我试图得到哪个时间更长的结果。我的意思是没有约会时间。
$open_time = date("g:i A", strtotime($restaurant['open_time']));
$close_time = date("g:i A", strtotime($restaurant['close_time']));
$curren_time = date("h:i");
$restaurant['open_time']
和$restaurant['close_time']
是数据库的结果。这是24小时格式,如22:30,10:20。
我想要什么。
if(($current_time > $open_time) && ($current_time < $close_time)
{
echo "Opened";
}
else
{
echo "Closed";
}
如果current_time
为2:00 AM
且open_time
为11:00 AM
,则结果应为已结束。如果current_time
为1:00 PM
且closed_time为11:00 PM
,则结果应为echo Opened。希望我解释得很好。如果您对我的问题有任何疑问,请询问。
您也可以使用24小时格式时间解决问题。但请记住,您可以添加日期,但实际上不是来自数据库你可以用来获得更长的时间
答案 0 :(得分:2)
设置正确timezone并且应该有效。
$restaurant = [
'open_time' => '11:00',
'close_time' => '22:00'
];
$timeZone = new DateTimeZone('Europe/Warsaw');
$now = new DateTime('now', $timeZone);
$open = DateTime::createFromFormat('H:i', $restaurant['open_time'], $timeZone);
$close = DateTime::createFromFormat('H:i', $restaurant['close_time'], $timeZone);
$working_now = ($now > $open && $now < $close);
if ($working_now) {
echo 'open';
} else {
echo 'closed';
}
您可以在sandbox中使用它 - 取消注释测试行以更改当前时间。
如果它在午夜开放,你可能需要额外的逻辑:
if ($open > $close) {
if ($now > $close) {
$close->modify('+1 day');
} else {
$open->modify('-1 day');
}
}
$working_now = ...