In my SQL Database i have a date column. I want to use a PHP if statement to show some text if the date from the DB equals todays date. Aswell as that, use an else to show something else if the date does not equal today.
Running PHP 7
I have lost count of what i have tried with this but don't seem to get anywhere.
I am fetching the data here:
$sql1 = 'SELECT `date` FROM `operations`.`opsroom` ORDER BY `opsroom`.`date` ASC, `opsroom`.`starttime` ASC LIMIT 1';
$nextsession = mysqli_query($conn, $sql1);
Later on in the file is where i am using this:
<?php
while ($row = mysqli_fetch_assoc($nextsession))
{
if( $row['date'] == DATE(date)){
echo "BOOKINGS TODAY";
} else {
echo "No Bookings";
}
}
?>
Only error i get at the moment is PHP Warning: Use of undefined constant date - assumed 'date' (this will throw an Error in a future version of PHP)
Use proper date
function of PHP:
If you want to compare today date then try below:
<?php
while ($row = mysqli_fetch_assoc($nextsession))
{
// this one you need to change
if( $row['date'] == date('Y-m-d')){
echo "BOOKINGS TODAY";
} else {
echo "No Bookings";
}
}
?>
You are using Date(date) where date acts as formatter which should not be the case.
Based on your db table's date column's format of date you have to use:
date("Y/m/d") or date("Y.m.d") or date("Y-m-d")
For eg. if the date in your table has format (Y/m/d) then your if statement should be:
if( $row['date'] == date(Y/m/d){
echo "BOOKINGS TODAY";
}
Use proper function for date in php. its date not DATE. recheck code
while ($row = mysqli_fetch_assoc($nextsession))
{
if( date('Y-m-d',strtotime($row['date'])) == date('Y-m-d')){
echo "BOOKINGS TODAY";
} else {
echo "No Bookings";
}
}