【问题标题】:Select records from date and ignore time portion not working从日期中选择记录并忽略时间部分不起作用
【发布时间】:2014-03-24 17:13:18
【问题描述】:

我的情况很奇怪。我使用内置日历类创建了一个带有 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。因此删除活动的地方不会影响查询。还有其他想法吗?
  • 顺便说一句,我尝试了所有删除活动部分的查询,仍然不起作用。
  • 您能否发布您使用 last_query() 获得的查询(检查答案)?

标签: php codeigniter datetime activerecord time


【解决方案1】:

我的建议是你使用

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

然后获取您针对 DDBB 提出的查询。有了这个,你就可以用你的 MySQL 客户端抛出查询,看看你是否真的得到了 CI 正在使用的查询的行。

事实上,你写的第一个代码块看起来不错,其他有一个$this-&gt;db-&gt;where('active', 1); 第一个没有,所以再次检查查询,XD。

【讨论】:

    猜你喜欢
    • 2014-12-18
    • 2016-03-15
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多