Using date() in recordset query PHP 7

This works:
$minutes = new WA_MySQLi_RS(“minutes”,$derry,0);
$minutes->setQuery(“SELECT * FROM MINUTES WHERE minDate >= ‘2021’ ORDER BY minDate DESC”);

But I want to use something like date(“Y”) instead of an actual year ‘2021’ so it changes automatically. I have tried many versions. Just need the right one. minDate is a meeting date like 2021-01-04. I want to show all meeting minutes that post in 2021. Then change >= to <= on another page to show all before 2021.

I’m using PHP 7.4.

If min date is a full date, you need to provide a full date for it to compare to.

date("Y-m-d") should work.

You should also check out the documentation for the class you’re using to ensure you’re escaping your dynamic queries correctly. It won’t matter too much for a generated parameter like this one where you know it won’t be malicious, but it’s best practice.

Hi @TJgamut:

Perhaps something like:

“SELECT * FROM MINUTES WHERE minDate >= YEAR(CURDATE()) ORDER BY minDate DESC” will do the trick.


That threw an error 500. This one basically the same as date(“Y”). This is PHP 7.4.

This one returned all of the records in the table, not just the ones from 2021. It’s in PHP 7.4

That’s odd… can you show me the result of SELECT YEAR(CURDATE())?

Another thing I’m wondering is what’s the data type of the field minDate.

If it’s a date you probably want to do something like “SELECT * FROM MINUTES WHERE YEAR(minDate) >= YEAR(CURDATE()) ORDER BY minDate DESC”

with YEAR(CURDATE()) it dumps the entire table. Should return top result only. Data type is date. I just need something to show current year. When I write in the year, ‘2021’ it works as expected. But, I don’t want to have to go in and change it annually.

This seems odd… if you issue a simple SELECT YEAR(CURDATE()) you should get the value 2021 which is what you originally had hardcoded… Only in this version it will adjust automatically in the future

Looks like I solved it by using a variable:

$stuff = date(“Y”);

$minutes = new WA_MySQLi_RS(“minutes”,$derry,0);
$minutes->setQuery(“SELECT * FROM MINUTES WHERE minDate >= ‘$stuff’ ORDER BY minDate DESC”);

I use this. Shows the year in 4 digits. Hope this is what you are looking for.

<?php echo ""; $year = date("Y"); echo $year; ?>
Sponsor our Newsletter | Privacy Policy | Terms of Service