从日期中选择记录并忽略时间部分不起作用

时间:2014-03-24 17:13:18

标签: php codeigniter datetime activerecord time

我有一种奇怪的情况。我使用内置日历类创建了一个带有codeigniter的日历。一切都可以抓住事件并将它们放在日历上,所以我知道选择日期有效......但现在我试图选择特定日期的记录,例如2014-03-28,但是无论我使用什么查询,它都不会从那天开始记录。我有一个表,start_date是我试图拉过的日期,它设置为dateTime。任何想法,我做错了,这是我尝试过的(start_date被赋予功能等于Y-m-d。如2014-03-28):

function get_list_events($start_date) {

    $start_date_start = date('Y-m-d', strtotime($start_date.' 00:00:00'));
    $start_date_end = date('Y-m-d', strtotime($start_date.' 23:59:59'));

    $this->db->where("start_date BETWEEN '$start_date_start%' AND '$start_date_end%'", NULL, FALSE);
    $query = $this->db->get('events');

    $data = array();

    foreach ($query->result() as $row) {
    $data[] = array(
        'id' => $row->id,
        'title' => $row->title,
        'description' => $row->description,
        'cost' => $row->cost,
        'image' => $row->image,
        'start_date' => $row->start_date,
        'end_date' => $row->end_date,
        'venue' => $row->venue,
        'venue_address' => $row->venue_address,
        'venue_city' => $row->venue_city,
        'venue_state' => $row->venue_state,
        'venue_zipcode' => $row->venue_zipcode,
        'contact_name' => $row->contact_name,
        'contact_email' => $row->contact_email,
        'contact_phone' => $row->contact_phone,
        'contact_website' => $row->contact_website,
        'create_date' => $row->create_date,
        'active' => $row->active,
    );
    }
    return $data;

}

也尝试了最明显的方式:

function get_list_events($start_date) {

    $start_date = date('Y-m-d', strtotime($start_date));

    $this->db->where('active', 1);
    $this->db->where('start_date', $start_date);
    $this->db->order_by('start_date', 'desc');
    $query = $this->db->get('events');

    $data = array();

    foreach ($query->result() as $row) {
    $data[] = array(
        'id' => $row->id,
        'title' => $row->title,
        'description' => $row->description,
        'cost' => $row->cost,
        'image' => $row->image,
        'start_date' => $row->start_date,
        'end_date' => $row->end_date,
        'venue' => $row->venue,
        'venue_address' => $row->venue_address,
        'venue_city' => $row->venue_city,
        'venue_state' => $row->venue_state,
        'venue_zipcode' => $row->venue_zipcode,
        'contact_name' => $row->contact_name,
        'contact_email' => $row->contact_email,
        'contact_phone' => $row->contact_phone,
        'contact_website' => $row->contact_website,
        'create_date' => $row->create_date,
        'active' => $row->active,
    );
    }
    return $data;

}

最后我尝试了这个:

function get_list_events($start_date) {

    $start_date = date('Y-m-d H:i:s', strtotime($start_date.' 00:00:00'));
    $start_time = date('Y-m-d H:i:s', strtotime($start_date.' 23:59:59'));

    $this->db->where('active', 1);
    $this->db->where('start_date >=', $start_date);
    $this->db->where('start_date <=', $start_time);
    $this->db->order_by('start_date', 'desc');
    $query = $this->db->get('events');

    $data = array();

    foreach ($query->result() as $row) {
    $data[] = array(
        'id' => $row->id,
        'title' => $row->title,
        'description' => $row->description,
        'cost' => $row->cost,
        'image' => $row->image,
        'start_date' => $row->start_date,
        'end_date' => $row->end_date,
        'venue' => $row->venue,
        'venue_address' => $row->venue_address,
        'venue_city' => $row->venue_city,
        'venue_state' => $row->venue_state,
        'venue_zipcode' => $row->venue_zipcode,
        'contact_name' => $row->contact_name,
        'contact_email' => $row->contact_email,
        'contact_phone' => $row->contact_phone,
        'contact_website' => $row->contact_website,
        'create_date' => $row->create_date,
        'active' => $row->active,
    );
    }
    return $data;

}

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我的建议是你使用

// ... Code above ...
$query = $this->db->get('events');
echo $this->db->last_query();
die();

获取您对DDBB投掷的查询。这样,您就可以向MySQL客户端抛出查询,看看您是否真的得到了CI正在使用的查询行。

事实上,您编写的第一个代码块看起来很顺利,而其他代码的$this->db->where('active', 1);代码是第一个代码,所以再次检查查询,XD。