CodeIgniter Forums
Select rows by getting year from MYSQL timestamp [SOLVED] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Select rows by getting year from MYSQL timestamp [SOLVED] (/showthread.php?tid=56677)



Select rows by getting year from MYSQL timestamp [SOLVED] - El Forum - 01-10-2013

[eluser]jentree[/eluser]
I am so close, yet so far...

I have a large list of items. Each item has a "timestamp" for its creation date.
I want to return a list of all items for any given year.
The timestamp looks like this:
Code:
2012-01-12 18:02:03

So my query looks like this:
Code:
$this->db->order_by('item_name', 'asc');
$this->db->where('DATE_FORMAT(timestamp, "%Y")', $year);

$query = $this->db->get('items');


if ( $query->num_rows() > 0 )
{
return $query->result();
}
else
{
return FALSE;
}

$year is passed into my function as an intiger (2012 for example)

Which produces this:
Code:
SELECT * FROM (`exhibitor_contracts`) WHERE DATE_FORMAT(timestamp, "%Y") '2012' ORDER BY `company_name` asc

I (somehow) just need to get the "=" in there right before the 2012. So my query ultimatly looks like this:
Code:
SELECT * FROM (`exhibitor_contracts`) WHERE DATE_FORMAT(timestamp, "%Y") = '2012' ORDER BY `company_name` asc

Thank you for your help!


Select rows by getting year from MYSQL timestamp [SOLVED] - El Forum - 01-10-2013

[eluser]Aken[/eluser]
Try putting it right in your WHERE clause.

Code:
$this->db->where('DATE_FORMAT(timestamp, "%Y") =', $year);



Select rows by getting year from MYSQL timestamp [SOLVED] - El Forum - 01-10-2013

[eluser]jentree[/eluser]
ARGH!!! That's about the only combination I didn't try. Thank you very much for your time/help!


Select rows by getting year from MYSQL timestamp [SOLVED] - El Forum - 01-10-2013

[eluser]CroNiX[/eluser]
Did you try using MySQLs YEAR() function?
Code:
$this->db->where('YEAR(timestamp)', $year);