【问题标题】:Cannot compare time in mysql and php无法比较mysql和php中的时间
【发布时间】:2019-06-11 10:19:22
【问题描述】:

我有两个下拉菜单,其中一个显示时间,我们可以从时间中选择,另一个是时间,它以以下格式显示时间

01:00 AM   09:00 AM

06:00 PM   07:00 PM

在我的数据库中,我有一个日期时间字段,它将日期时间保持在 2019-06-03 17:15:00 这种格式。我 如果有人选择了特定的时间范围,那么应该显示这些记录,但无法理解如何编写查询,因为下拉列表中的时间格式不同(上午或下午)

【问题讨论】:

  • 您需要将 7PM 转换为 19:00:00 并与数据库中的记录进行比较。
  • 在 php 中尝试date("g:i a", strtotime("2019-06-03 17:15:00 "));。如果你想在 JS 中做它有点复杂。
  • 我如何将 7PM 转换为 19:00:00
  • 或者,学习一些 HTML 基础知识 - 并意识到选择选项的 submission value 可能与您想要的 text 不同显示……

标签: php mysql


【解决方案1】:

您最常将 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"));

【讨论】:

    【解决方案2】:

    你需要做两步:

    $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'";
    

    【讨论】:

    • 我正在像这样比较日期 $selectedFromTime = date("H:i:s", strtotime("2019-06-13 06:45:00")); $selectedToTime = date("H:i:s", strtotime("2019-06-13 09:00:00")); $this->db->where('start>=',$selectedFromTime); $this->db->where('end
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2011-09-06
    • 1970-01-01
    相关资源
    最近更新 更多