用于在数据库中随时比较的PHP函数

用于在数据库中随时比较的PHP函数,php,mysql,time,Php,Mysql,Time,我试图根据数据库中记录的时间显示不同的内容 从下午18点到早上8点,播放内容B。 其余时间显示内容A 我的数据库字段存储的是时间字段,所以在数据库中,1800 pm存储为18:00:00,同样到了上午8:00,它存储为08:00:00 以下是我在18:00:00到08:00:00之间获取内容B的逻辑: $now = strtotime(date('H:i:s')); $time_from = strtotime($data['date_from']); //18:00:00 $time_to =

我试图根据数据库中记录的时间显示不同的内容

从下午18点到早上8点,播放内容B。 其余时间显示内容A

我的数据库字段存储的是时间字段,所以在数据库中,1800 pm存储为18:00:00,同样到了上午8:00,它存储为08:00:00

以下是我在18:00:00到08:00:00之间获取内容B的逻辑:

$now = strtotime(date('H:i:s'));
$time_from = strtotime($data['date_from']); //18:00:00
$time_to = strtotime($data['date_to']); // 08:00:00
if($now >= $time_from && $now <= $time_to){
   echo 'content A';
}
$now=strottime(日期('H:i:s');
$time\u from=strottime($data['date\u from'])//18:00:00
$time_to=strotime($data['date_to']);//08:00:00

如果($now>=$time\u from&&$now
if(date(“H:i”)>='08:00'&&date(“H:i”)在进行比较之前,您还需要输入
$now
变量,请尝试:

$now = date('H:i:s');
$time_from = strtotime($data['date_from']); //18:00:00
$time_to = strtotime($data['date_to']); // 08:00:00
$now_str = strtotime($now);
if($now_str >= $time_from && $now_str <= $time_to){
   echo 'content A';
}
$now=date('H:i:s');
$time\u from=strottime($data['date\u from']);//18:00:00
$time_to=strotime($data['date_to']);//08:00:00
$now_str=strtime$now;

如果($now\u str>=$time\u from&&$now\u str为什么不使用Carbon Library来操纵日期这很简单,我确实在$now中添加了strotime。我的问题中遗漏了这一部分。因此我编辑了它。即使使用strotime,也无助于解决问题。假设现在的时间是23:30:00。unix时间戳始终大于08:00:00.08:00:00应该是这种方式将在@Kimberlee起作用-这只是不同于您尝试的逻辑。确定何时显示内容A,在其他时间显示内容B。是的。它起作用。我认为反向逻辑在我的情况下不起作用。我已经对代码和逻辑进行了更改。谢谢Danmullen!
$now = date('H:i:s');
$time_from = strtotime($data['date_from']); //18:00:00
$time_to = strtotime($data['date_to']); // 08:00:00
$now_str = strtotime($now);
if($now_str >= $time_from && $now_str <= $time_to){
   echo 'content A';
}