Home > Enterprise >  Check if today meets a date in tablerow at SQL
Check if today meets a date in tablerow at SQL

Time:01-09

I have a SQL table with 20 rows. Each row contains a columd 'datum'(date) and a few other data. To view some information on the website, only when the date of today meets one of the entered dates in the SQL table, I had placed a 'if'.

if ($date_now = $feestdag) {
    echo '<i style="font-weight: bold; color: red;">YESS</i>';
}else{
    echo 'Nope</i>';
}

The variables used are:

 $date_now = date("Y-m-d");
                $resultalert = mysqli_query($conn,"SELECT datum FROM feestdagen") or die(mysql_error());
                while($feestdagenalert = mysqli_fetch_array($resultalert)) {
                    $feestdag = $feestdagenalert['datum'];
//removed some data
                }

Now, I get everytime the text what I only want to see when the date of today meets ONE of the 20 entries in the table 'feestdagen' at column 'datum'. Column 'datum' has entries like '2022-01-02', '2022-05-22' et cetera.

Does someone know the solution to make this work? I couldn't find it.. Thanks for help!

Edited: used the solution of khalil and works perfect

CodePudding user response:

SELECT datum FROM feestdagen WHERE datum = CURDATE();

This will only return that row which has the current date. You don't need to perform a separate check in PHP

CodePudding user response:

You can just

SELECT EXISTS (SELECT *
                      FROM feestdagen
                      WHERE datum = curdate()) AS isfeestdag;

You'll get 1, if today is a holiday, 0 otherwise. Have an index on datum, unless feestdagen contains only a few rows and this will be blazing fast.

  •  Tags:  
  • Related