2017-03-01 103 views
0

我目前正在执行电子商务系统。我想监测当月有多少人购买了特定产品。例如,我正在销售耳机,5月份购买1月份的耳机,2月份等等。使用Codeigniter计算特定月份的所有月份

问题:是否可以在我的表中将存储值中的mm分开?

注:我用在HTML中的日期选择器(在HTML日期选择的格式为YYYY-MM-DD)

CONTROLLER

$data['products'] = $this->CrudModel->count_all('products','date'); 
print_r($data['products']);die; 

MODEL

public function count_all($table,$date) 
{ 
    $this->db->select('product_name'); 
    $this->db->from($table); 
    $this->db->where(array('date' => '$date')); // What should I put here? So that I can fetch the date starting January until December 
    $num_results = $this->db->count_all_results(); 
} 

我的PRODUCT产品列表

'event_id' 'full_name' 'email' 'date' 'contact' 
+0

您可以通过使用组。 – shafiq

+0

对不起,对于最近的回复..先生? – Angel

回答

0

请尝试以下代码。

$this->db->where('date >=', $start_date); 
$this->db->where('date <=', $end_date); 

,或者您也可以尝试

$where = "DATE(date) BETWEEN '2017-01-01' AND '2017-02-01'"; 
+0

先生有可能在我使用日期选择器的存储值中拆分mm吗? – Angel

+0

是的,这是可能的... –

0

您可以像使用组的下方

public function count_all($table,$date) 
{ 
    $this->db->select('product_name'); 
    $this->db->from($table); 
    $this->db->where('date >=', $start_date); 
    $this->db->where('date <=', $end_date); 
    $this->db->order_by('date','desc'); 
    $this->db->group_by('MONTH(date), YEAR(date)'); 
    $num_results = $this->db->count_all_results(); 
} 
+0

对不起,最近的回复..这是什么意思? “('MONTH(date),YEAR(date)')” – Angel

+0

这些是mysql日期函数,月份(日期)将返回日期和年份的月份将返回年份。您可以将您的商店日期分为毫米和YY。欲了解更多信息。 https://dev.mysql.com/doc/refman/5.5/zh/date-and-time-functions.html http://stackoverflow.com/questions/7830987/how-do-i-extract-month -and-year-in-a-mysql-date-and-compare-them – shafiq

+0

先生,我已经试过这个,但没有任何价值。 – Angel