0

我在我的数据库中有一个名为访问的表,如下所示:

id ip action_date|time_stamp

我使用此代码来存储站点访问

/* Hits table has an auto-incrementing id and an ip field */

        // Grab client IP
        $ip = $this->input->ip_address();

        // Check for previous visits
        $query = $this->db->get_where('visits', array('ip' => $ip), 1, 0);
        $query = $query->row_array();

        if (count($query) < 1 )
        {
            // Never visited - add
            $this->db->insert('visits', array('ip' => $ip) );
        } 

它工作得很好。但我的客户需要知道他们一个月有多少次访问。我怎样才能做到这一点 ?坦克。

4

1 回答 1

1

怎么样:

$months = $this->db->select('
    COUNT(`id`) AS "count", 
    DATE_FORMAT(FROM_UNIXTIME(`action_date`),"%b-%Y") AS "month"',false
)
->group_by('month')
->where(array('ip' => $ip))
->order_by('action_date','desc')
->get('visits')->result();

应该给你这样的数据:

+-------+----------+
| count | month    |
+-------+----------+
|   505 | Apr-2010 |
|   252 | Mar-2010 |
|   426 | Feb-2010 |
|   201 | Jan-2010 |
|   211 | Dec-2009 |
+-------+----------+
于 2010-04-09T08:56:51.800 回答