I have a table called orders
with orderID
, StartDate
, EndDate
, etc.
I want to find if today's date falls within StartDate
and EndDate
. As a starting point I want to print the result that apply
This is my code I just cannot make it work. Also, CURDATE()
does not work for me.
<?php
//$originalDate = "2010-03-21";
//$newDate = date("d-m-Y", strtotime($originalDate));
include "dbh.php";
$today = DATE("Y-m-d");
// WHERE $today > StartDate OR $today < EndDate OR $today > SuspendEnd
OR $today < SuspendStart";
//{
// echo $today;
//}
$sql= "SELECT * FROM orders
WHERE $today = StartDate
";
$records = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($records)){
echo "<tr>";
echo "<td>".$row['OrderID']."</td>";
//echo "<td>".$row['StartDate']."</td>";
echo "</tr>";
}
?>
I dont have mysql running to test this out but you can try
select orderID from orders where (CURDATE() between StartDate and EndDate)
You can just use the following SQL query to get all orders current:
SELECT
*
FROM
orders
WHERE
CURRENT_DATE() BETWEEN StartDate AND coalesce(EndDate, CURRENT_DATE) ;
It uses the fact that MySQL already has a function to represent "today" (so, you don't need to pass today's value from PHP). If your "EndDate" can be NULL, and you want it to mean "never ends", using COALESCE
to today's date will make the previous query work.
Check everything at dbfiddle here
References: