deviloper
deviloper

Reputation: 7240

get YEAR from datetime format in mysql using codeigniter active record

I have a codeigniter application and I need to get the year from a mysql field with datetime datatype. I need to do this using CI active record using the following way.

$this->db->select(YEAR("added_date"));
$this->db->from("news");
$this->db->order_by("added_date", "DESC");
$this->db->where('published', 1);
$this->db->distinct();
$this->db->get();

but I get the following error:

Call to undefined function YEAR() in /opt/lampp/htdocs/...

I use codeigniter 2.1.x (if needed)

Upvotes: 0

Views: 4600

Answers (1)

Prix
Prix

Reputation: 19528

YEAR is a MYSQL function not codeigniter function:

$this->db->select('YEAR(`added_date`)');

Upvotes: 2

Related Questions