Sahar Alsadah Sahar Alsadah - 5 months ago 16
HTML Question

Compare My DB data against specific date

My database data are table of dates. I want to create a list to report entries of expired dates.
My Database

for that, I used the following php script:

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);

$sql = "SELECT Batch#, Date1, Date2 FROM users";
$result = $conn->query($sql);
$now= strtotime("-1 Months");
if ($result->num_rows > 0) {
$Date1= $row["Date1"];
$Date2= $row["Date2"];
echo "<table id='t1'><tr><th>Alert</th><th>Action</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
if($Date1 >= $now)
echo "<tr><td>One month or less to expiry date of product with batch# " . $row["Batch#"]. "</td><td>Please take an action</td></tr>";
echo "</table>";
} else {
echo "0 results";

I kept getting an error: "Notice: Trying to get property of non-object in C:\xampp\htdocs\Testing\AlertTest.php on line 17
0 results"

I couldn't figure out what that means or how to fix it. I've been this method before and this is the first time I get such an error.


As you are comparing the dates, you have to mind that the both dates are in same format (Date string or time stamp). For comparison both the format as timestamp is best. So using the PHP function timestamp() just convert your database time to a unique number.

Your comparison:

if($Date1 >= $now){

Here The $Date1 is a string like '2016-07-07' and the $now something like '1468057029', so the condition not goes correct / success. What you have to do is just convert both time into timestamp and execute the comparison.

$Date1 = strtotime($row["Date1"]);
$Date2 = strtotime($row["Date2"]);

Hope this helped you.