将发布日期与codeigniter中的时间戳进行比较

时间:2014-08-07 10:14:02

标签: php mysql sql codeigniter date

我有registered_customers_bod 功能,我希望在两个日期之间得到结果。因为我正在使用的地方和Clause。但我面临一个问题,数据库中的记录包含一个时间戳,我发布日期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();
    }

3 个答案:

答案 0 :(得分: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();

希望这有帮助!

答案 1 :(得分:1)

这是解决方案......

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

希望这可以帮助你......

答案 2 :(得分:0)

您可以使用php函数strtotime()将其转换为时间戳:

$timestamp_to = strtotime($this->input->post('to'));