如何在codeigniter活动记录中选择MONTH()和YEAR()

时间:2015-04-17 04:50:02

标签: php mysql codeigniter

我在codeigniter工作,我的问题是我想选择month&年份在下面的查询..它返回月份,但我需要月和&一年作为单一价值..请帮助..

public function get_content(){
        $this->db->select('MONTH(newsroom_publish_date) as month');
        $this->db->from('content_table');
        $this->db->where('status', "Active");
        $query = $this->db->get();
        return $query->result();
    }

2 个答案:

答案 0 :(得分:2)

MONTH中的MySQL功能相同,使用YEAR功能。

这样做:

public function get_content(){
        $this->db->select('MONTH(newsroom_publish_date) as month,  YEAR(newsroom_publish_date) as year');
        $this->db->from('content_table');
        $this->db->where('status', "Active");
        $query = $this->db->get();
        return $query->result();
    }

YEAR从给定的时间戳返回。

让我知道更多帮助!

答案 1 :(得分:2)

试试这个:

public function get_content(){
        $this->db->select('CONCAT(MONTH(newsroom_publish_date)),'-',(YEAR(newsroom_publish_date)) as year_month);
        $this->db->from('content_table');
        $this->db->where('status', "Active");
        $query = $this->db->get();
        return $query->result();
    }
相关问题