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.
<?php
 $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

LVL 7
rgranlundAsked:
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,

YEAR(NOW())

So try this.

 SELECT meta_value AS value
 FROM wp_connections
 WHERE FROM_UNIXTIME(date_added, "%Y") = YEAR(NOW())
0
 
rgranlundAuthor Commented:
That was simple enough! Thanks.
0
 
_agx_Commented:
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'
0
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.