如何在执行以下查询到codeigniter
后的最后十天获取日期public function get_data() {
$this->db->select('*');`enter code here`
$this->db->select_sum('total_sale');
$this->db->where('store_date BETWEEN CURDATE() - INTERVAL 7 DAY AND CURDATE()');
$query = $this->db->get('one_month_report');
if ($query->num_rows() > 0) {
return $query->result();
} else {
return FALSE;
}
}
答案 0 :(得分:2)
如果您的数据库将这些值存储为DATETIME或TIMESTAMP,则应该可以正常工作。
$start_date = date("Y-m-d 00:00:00", strtotime("-1 week"));
$end_date = date("Y-m-d 59:59:59");
$this->db->where("store_date >= '" . $start_date . "' AND store_date <= '" . $end_date . "'");
答案 1 :(得分:0)
试试这个
public function get_data()
{
$query = $this->db->query("SELECT *, SUM(cast(REPLACE(total_sale, ',', '') as decimal(8,2)))
FROM one_month_report
WHERE store_date BETWEEN CURDATE() AND DATE(NOW()) - INTERVAL 7 DAY");
$result= $query->result_array();
if (empty($result)) {
echo "Empty result";
} else {
return $result;
}
}
确保
CURDATE()
返回日期
答案 2 :(得分:0)
请试试这个:
$this->db->select('*');
$this->db->select_sum('total_sale');
$this->db->where('store_date BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()');
$query = $this->db->get('one_month_report');
答案 3 :(得分:0)
这对我有用..
$this->db->from('backbonedb');
$this->db->where('status', $status);
$this->db->where('date <=', date('Y-m-d'));
$this->db->where('date >=', date('Y-m-d', strtotime('-7 days')));
$this->db->order_by("id", "DESC");
$query = $this->db->get();