Compare two dates in Codeigniter and MySQL

廉价感情. 提交于 2019-12-11 00:49:27

问题


How can I get values between two dates in Codeigniter query function? Here is my model and sample code.

function get_promo() {
    $today = date('Y-m-d');
    $query = $this->db->query('SELECT FROM tbl_event WHERE event_id = $id AND event_startdate <= $today
    AND event_enddate >= $today');
    return $query;
}

But it doesn't work, here is the error I got

A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM tbl_event WHERE event_id = 36 AND event_startdate <= 2011-06-09 ' at line 1

SELECT FROM tbl_event WHERE event_id = 36 AND event_startdate <= 2011-06-09 AND event_enddate >= 2011-06-09

Filename: C:\xampp\htdocs\hotel\system\database\DB_driver.php

Line Number: 330

回答1:


I think you need qoutes around your date (i.e. '2011-06-08'). try this

function get_promo() {
    $today = date('Y-m-d');  
    $query = $this->db->query(
        "SELECT FROM tbl_event WHERE event_id = {$id} AND event_startdate <= '{$today}'
        AND event_enddate >= '{$today}'");
    return $query;
}

If your columns event_startdate and event_enddate are DATETIME type but you are only interested in the date part you can do `DATE(event_enddate) to extract the date part




回答2:


I think u need to user date_format(), more information in this link http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format.

Try this code:

$today = date('Y-m-d');
$query = $this->db->query("SELECT FROM tbl_event WHERE event_id = $id AND DATE_FORMAT(event_startdate ,'%Y-%m-%d') >= DATE_FORMAT($today ,'%Y-%m-%d') AND DATE_FORMAT(event_enddate ,'%Y-%m-%d') <= DATE_FORMAT($today ,'%Y-%m-%d')");



回答3:


Looking into the errors, it seems that your queries are not escaped properly. Add single or double quotes to fix it. Check @danneth answer. Use Query Binding which is simple and secure or even more use Active Record. Here are examples of both binding and active record based on your code.

Query Binding Example

    $today = date('Y-m-d');
    $sql = 'SELECT 
  * 
FROM
  tbl_event 
WHERE event_id = ? 
  AND event_startdate <= ? 
  AND event_enddate >= ?';
    $query = $this->db->query($sql, array($id, $today, $today));
return $query;

Active Record Example

$query = $this->db
        ->select('*')->from('tbl_event')
        ->where(array(
            'event_id' => $id,
            'event_startdate <= ' => $today,
            'event_enddate >= ' => $today
        ))
        ->get();
return $query;

Append ->result() or ->result_array() etc to get the result in an object or array. Look into the Codeigniter Database User Guide for more. Happy coding!




回答4:


$this->db->where('date_start <=',date('Y-m-d'));
$this->db->where('date_end >=',date('Y-m-d'));
$query = $this->db->get('table');



回答5:


You need to use the right format. Try this:

$todaystart = date('Y-m-d 00:00:00');  
$todayend = date('Y-m-d 23:59:59');  
       $query = $this->db->query('SELECT FROM tbl_event WHERE event_id = $id AND event_startdate <= ? AND event_enddate >= ?', array($todaystart, $todayend));

EDIT: your query is wrong. Do this:

$todaystart = date('Y-m-d 00:00:00');  
$todayend = date('Y-m-d 23:59:59');  
       $query = $this->db->query('SELECT * FROM tbl_event WHERE event_id = $id AND event_startdate <= ? AND event_enddate >= ?', array($todaystart, $todayend));


来源:https://stackoverflow.com/questions/6288261/compare-two-dates-in-codeigniter-and-mysql

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