在Codeigniter和MySQL中比较两个日期

nfs0ujit  于 2022-12-25  发布在  Mysql
关注(0)|答案(5)|浏览(126)

如何在Codeigniter查询函数中获取两个日期之间的值?下面是我的模型和示例代码。

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;
}

但它不工作,这是我得到的错误

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
khbbv19g

khbbv19g1#

我想你需要在你的日期(iidoEe.“2011-06- 08”)周围的qoutes。试试这个

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;
}

如果列event_startdateevent_enddateDATETIME类型,但您只对日期部分感兴趣,则可以执行'DATE(event_enddate)来提取日期部分

sycxhyv7

sycxhyv72#

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

5ssjco0h3#

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.
请尝试以下代码:

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

jckbn6z74#

您的查询存在问题,因为它们未正确转义。若要解决此问题,请尝试添加单引号或双引号。您还可以考虑使用查询绑定或活动记录,这两种方法都是简单而安全的查询处理方法。@danneth answer在这方面可能会有所帮助。以下是一些如何基于您的代码使用绑定和活动记录的示例。

查询绑定示例

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

活动记录示例

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

追加->result()->result_array()等以获取对象或数组中的结果。查看Codeigniter Database User Guide以了解更多信息。

hxzsmxv2

hxzsmxv25#

您需要使用正确的格式。请尝试以下操作:

$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));

编辑:您的查询错误。请执行以下操作:

$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));

相关问题