将发布日期与codeigniter中的时间戳进行比较
Posted
技术标签:
【中文标题】将发布日期与codeigniter中的时间戳进行比较【英文标题】:Compare post date with timestamp in codeigniter 【发布时间】:2014-09-30 12:53:12 【问题描述】:我有registered_customers_bod
函数,我想在两个日期之间得到结果。为此,我正在使用 to 和 from where 子句。但我面临一个问题,数据库中的记录包含一个时间戳,我发布的日期为 YYYY-MM-DD 格式。谁能帮我把这个时间戳记为 YYYY-MM-DD 格式或任何其他解决方案。
谢谢!!!!
function registered_customers_bod()
$this->db->select(array(
'tbl_customer_registration.cus_name',
'tbl_customer_registration.cus_email',
'tbl_customer_registration.cus_phone',
'tbl_customer_registration.cus_mobile',
'tbl_customer_registration.cus_addr_no',
'tbl_customer_registration.cus_addr_street',
'tbl_customer_registration.cus_addr_city',
'tbl_user_registration.user_status',
'tbl_user_registration.user_reason_status',
'tbl_user_registration.user_timestamp',
));
$this->db->from('tbl_customer_registration');
$this->db->join('tbl_user_registration', 'tbl_user_registration.user_id=tbl_customer_registration.user_id');
$this->db->group_by("tbl_customer_registration.cus_id"); //view single record that contain two contact numbers
$this->db->where('user_timestamp >=',$this->input->post('to'));
$this->db->where('user_timestamp <=',$this->input->post('from'));
$query = $this->db->get();
return $query->result();
【问题讨论】:
【参考方案1】:下面会将日期字符串转换为 DateTime 对象,然后您可以使用该对象获取时间戳。
$to = new DateTime($this->input->post('to'));
$from = new DateTime($this->input->post('from'));
$this->db->select(array(
'tbl_customer_registration.cus_name',
'tbl_customer_registration.cus_email',
'tbl_customer_registration.cus_phone',
'tbl_customer_registration.cus_mobile',
'tbl_customer_registration.cus_addr_no',
'tbl_customer_registration.cus_addr_street',
'tbl_customer_registration.cus_addr_city',
'tbl_user_registration.user_status',
'tbl_user_registration.user_reason_status',
'tbl_user_registration.user_timestamp',
));
$this->db->from('tbl_customer_registration');
$this->db->join('tbl_user_registration', 'tbl_user_registration.user_id=tbl_customer_registration.user_id');
$this->db->group_by("tbl_customer_registration.cus_id"); //view single record that contain two contact numbers
$this->db->where('user_timestamp >=',$to->getTimestamp());
$this->db->where('user_timestamp <=',$from->getTimestamp());
$query = $this->db->get();
return $query->result();
希望这会有所帮助!
【讨论】:
【参考方案2】:这是解决方案...
function registered_customers_bod()
$this->db->select(array(
'tbl_customer_registration.cus_name',
'tbl_customer_registration.cus_email',
'tbl_customer_registration.cus_phone',
'tbl_customer_registration.cus_mobile',
'tbl_customer_registration.cus_addr_no',
'tbl_customer_registration.cus_addr_street',
'tbl_customer_registration.cus_addr_city',
'tbl_user_registration.user_status',
'tbl_user_registration.user_reason_status',
'tbl_user_registration.user_timestamp',
));
$this->db->from('tbl_customer_registration');
$this->db->join('tbl_user_registration', 'tbl_user_registration.user_id=tbl_customer_registration.user_id');
$this->db->group_by("tbl_customer_registration.cus_id"); //view single record that contain two contact numbers
$this->db->where('STR_TO_DATE(user_timestamp, "%Y-%m-%d") >=',$this->input->post('to'));
$this->db->where('STR_TO_DATE(user_timestamp, "%Y-%m-%d") <=',$this->input->post('from'));
$query = $this->db->get();
return $query->result();
希望对你有所帮助....
【讨论】:
这个答案会起作用,但是,我会尽量避免在 where 子句中使用 SQL 函数,因为这可能会影响性能。以上是关于将发布日期与codeigniter中的时间戳进行比较的主要内容,如果未能解决你的问题,请参考以下文章
如何将 Unix 纪元时间戳与 SQL 中的 DATE 进行比较?