How to get the data using >= AND <= in a 12hr time format in php/mysql?

我的未来我决定 提交于 2019-12-24 18:06:09

问题


I want to get the time from 12:00:00 PM to 02:00:00 PM but it doesn't work when I use start_time >= '12:00:00' AND end_time <= '02:00:00'. I have this code so far.
Note: I'm using Code Igniter.

    $start =  date("g:i:s", strtotime($this->input->post('start')));
    $end =  date("g:i:s", strtotime($this->input->post('end')));
    $date_reserve =  $this->input->post('date_reserve');

    $sql = "SELECT materialID FROM schedule WHERE date_reserve = ? AND start_time >= ? AND end_time <= ?";
    $query = $this->db->query($sql,array($date_reserve,$start,$end));

I use g:i:s to format it in a 12hr time format. And when I try to echo it, gives me 12:00:00 and 02:00:00 it doesn't get the AM/PM. And also the data type of start_time and end_time in my DB is time. What is the proper way to retrieve the data and what data type should I use in my DB. Thanks!.


回答1:


Assuming that you use the DATETIME-format as datatype in your database this should help you:

$start =  date("h:i:s", strtotime($this->input->post('start')));
$end =  date("h:i:s", strtotime($this->input->post('end')));
...
$sql = "SELECT materialID FROM schedule WHERE date_reserve = ? AND TIME(start_time) >= ? AND TIME(end_time) <= ?";

Edit: So if you use TIME-format you only have to change the format of the values you are inserting:

$start =  date("h:i:s", strtotime($this->input->post('start')));
$end =  date("h:i:s", strtotime($this->input->post('end')));


来源:https://stackoverflow.com/questions/18531453/how-to-get-the-data-using-and-in-a-12hr-time-format-in-php-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!