无法比较mysql和php中的时间

时间:2019-06-11 10:19:22

标签: php mysql

我有两个下拉菜单,其中一个显示时间,我们可以选择一个时间,另一个用于显示时间,格式如下:

01:00 AM   09:00 AM

06:00 PM   07:00 PM

在我的数据库中,我拥有datetime字段,该字段将datetime保留在这种格式的2019-06-03 17:15:00中。一世 想知道是否有人选择了特定的时间范围,那么这些记录应该显示,但是不知道如何编写查询,因为下拉菜单中的时间格式不同(上午或下午)

2 个答案:

答案 0 :(得分:0)

您最多将7:00 PM转换为19:00:00以便存储在数据库中

date("Y-m-d H:i", strtotime("2019-06-11 07:00 PM"));

并在使用时将19:00:00转换为7:00 PM

date("Y-m-d g:i a", strtotime("2019-06-11 19:00:00"));

答案 1 :(得分:0)

您需要分两个步骤进行操作:

$selectedTime = $_POST['time']; // suppose it contains 19:00:00
$currentDate  = date('Y-m-d');
$datetime = $currentDate.' '.$selectedTime;
$query = "select * 
          from tablename
          where fieldname M= '".$datetime ."'";
// query is "select * from tablename where fieldname <= '2019-06-11 19:00:00'";