PHP MySQL Get Time stamped records from Current Year

How do I get records for the current year ? The following does not work.  I have a UNIX Time stamped field in my table.  I need all records in the current year.
 $get_id = $wpdb->get_results('SELECT term_id AS id, name AS name FROM wp_connections_terms WHERE term_id != 1');
        foreach ($get_id as $row) {
            $name = $row->name;
            $id = $row->id;

            $taxid = $wpdb->get_results("
          SELECT meta_value AS value 
          FROM wp_connections
          WHERE date_added > UNIX_TIMESTAMP(NOW(), INTERVAL 1 YEAR) );

Open in new window

Who is Participating?
NorieConnect With a Mentor VBA ExpertCommented:
Is date_added the UNIX_TIMESTAMP field?

If it is you can get the year from it like this,

FROM_UNIXTIME(date_added, "%Y")

and you could get the current year like this,


So try this.

 SELECT meta_value AS value
 FROM wp_connections
 WHERE FROM_UNIXTIME(date_added, "%Y") = YEAR(NOW())
rgranlundAuthor Commented:
That was simple enough! Thanks.
Just a suggestion. If the date_added column is indexed, best to avoid using functions on it the WHERE clause because it prevents the db from using the index. Instead, it's better to use something like this:

 WHERE date_added >= UNIX_TIMESTAMP(MAKEDATE(Year(curDate()), 1))
 AND      date_added < UNIX_TIMESTAMP(MAKEDATE(Year(curDate())+1, 1))

... which in other words means

 WHERE date_added >= '2018-01-01'
 AND      date_added < '2019-01-01'
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.