简体   繁体   English

使用CodeIgniter在数据库中使用日期格式对每月,每年的日期进行计数

[英]count date per month , per year on database with date format with CodeIgniter

how to calculate the amount of data in 1bulan / 1 year on the basis of the date in the database based on the date format?i am using codeigniter 如何基于日期格式基于数据库中的日期计算1bulan / 1年的数据量?我正在使用Codeigniter

Example

date 2016-02-10 日期2016-02-10

2016-05-19 2016-05-19

2016-07-20 2016-07-20

2016-02-30 2016-02-30

2016-05-20 2016-05-20

2016-05-21 2016-05-21

Result count 结果计数

data on Mounth Feb = 2 Mounth Feb的数据= 2

data on Mounth May = 3 Mounth May的数据= 3

data on Mounth Jul = 1 Mounth Jul上的数据= 1

This My script 这是我的剧本

<?php 
        $sql = "SELECT * FROM tbcounter ";
        $get_monthly = " WHERE date LIKE '".date("Y-m")."%'";
        $monthly = mysql_num_rows(mysql_query($sql . $get_monthly));    
        echo $monthly;                      
?>

You can use DATE_FORMAT( date_column ,'desired_output') as in this example:- You can read about date function on http://php.net/manual/en/function.date.php 您可以在此示例中使用DATE_FORMAT( date_column ,'desired_output'):-您可以在http://php.net/manual/en/function.date.php上阅读有关date函数的信息。

$date_variable = date("Y-m",strtotime('last month')); //prints 2016-01 
echo   $this->db->get_where("tbcounter",array("DATE_FORMAT(`date`,'%Y-%m')"=> $date_variable ))->num_rows();

This is demo code :) 这是演示代码:)

For Counting data per year and per month you can use 对于每年和每月的数据计数,您可以使用

GROUP BY YEAR(date), MONTH(date)

In your query. 在您的查询中。

Example : 范例:

SELECT count( * )
FROM `TableNAme`
GROUP BY YEAR( `Date` )
LIMIT 0 , 30


SELECT count( * )
FROM `TableNAme`
GROUP BY MONTH( `Date` )
LIMIT 0 , 30

Here Date is your column name in table. Date是表中的列名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM