问题
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