比较 Codeigniter 和 MySQL 中的两个日期

发布于 2024-11-14 21:11:20 字数 812 浏览 4 评论 0 原文

如何在 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

最终幸福 2024-11-21 21:11:20

我认为你需要围绕你的日期进行引用(即“2011-06-08”)。试试这个

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) 来提取日期部分

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

我们的影子 2024-11-21 21:11:20
$this->db->where('date_start <=',date('Y-m-d'));
$this->db->where('date_end >=',date('Y-m-d'));
$query = $this->db->get('table');
$this->db->where('date_start <=',date('Y-m-d'));
$this->db->where('date_end >=',date('Y-m-d'));
$query = $this->db->get('table');
指尖上的星空 2024-11-21 21:11:20

我认为您需要使用 date_format(),此链接中的更多信息 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')");

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')");
江心雾 2024-11-21 21:11:20

您的查询存在问题,因为它们没有正确转义。要解决此问题,请尝试添加单引号或双引号。
您还可以考虑使用查询绑定Active Record,都是处理查询的简单而安全的方法。 @danneth 的回答在这方面可能会有所帮助。
以下是如何使用绑定和 Active Record 的一些示例根据你的代码。

查询绑定示例

    $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 示例

$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 数据库用户指南

There are issues with your queries because they are not correctly escaped. To fix this, try adding single or double quotes.
You can also consider using Query Binding or Active Record, both simple and secure methods for handling queries. @danneth answer may be helpful in this regard.
Here are some examples of how to use 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.

居里长安 2024-11-21 21:11:20

您需要使用正确的格式。试试这个:

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

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文