3

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
2

6 Answers 6

7

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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

1 Comment

Thanks for the answer I tried it but I didn't get result.. . I do have a event_start 2011-06-09 and even_enddate 2011-06-30 but it doesn't show
1

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.

Comments

0

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

1 Comment

It's now working but I didn't get any query i do have on my database event_start 2011-06-09 and even_enddate 2011-06-30 but it doesn't show
0

Because databases can be used to access the current date, you only really should to pass in the PHP variable $id as a parameter. In truth, after you add the missing * between SELECT and FROM and leverage CURRENT_DATE twice, if $id in integer, your query won't break because integer values don't need quoting.

public function getPromo(int $id): array
{
    return $this->db->query('
        SELECT *
        FROM tbl_event
        WHERE
            event_id = ?
            AND event_startdate <= CURRENT_DATE
            AND event_enddate >= CURRENT_DATE
    ', [$id])
    ->result();
}

Or with query builder methods, you can freely use variables because identifiers and values will be automatically quoted/escaped (unless you turn auto-quoting off with false):

public function getPromo(int $id): array
{
    return $this->db
        ->where('event_id', $id)
        ->where('event_startdate <=', 'CURRENT_DATE', false)
        ->where('event_enddate >=', 'CURRENT_DATE', false)
        ->get('tbl_event')
        ->result();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.