• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Select rows by getting year from MYSQL timestamp [SOLVED]

#1
[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!

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

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

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

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.