comparing dates

1Tsme1941

Member
Hi, still fumbling. I have records in a database with an expiration field in DATE format.
I want to display messages according to how long to expiration. please, someone, check my
code. thanks.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "homedb";

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

$id=$row['id'];
$password=$row['password'];
$expiredate=$row['expiredate'];

while($row=mysql_fetch_array($result))
{

if(expiredate == expiredate - 7)
echo "<br/>1 week remaining"; }

else (expiredate == expiredate - 1)
{ echo "<br/>1 week remaining"; }

else (expiredate == today());
{ echo "<br/>we're sorry, your contract has expired"; }

?>
 

strollin

Well-Known Member
Your code doesn't handle the cases where the expiration date is 1-5 days away. It also doesn't handle cases where the expiration date has already passed.
 

1Tsme1941

Member
amI coding these wrong?
--------------------------------------------------

else (expiredate == expiredate - 1)
{ echo "<br/>1 week remaining"; }

else (expiredate == today());
{ echo "<br/>we're sorry, your contract has expired"; }
 

strollin

Well-Known Member
(expiredate == expiredate - 1) <- This will only happen when the expiration date is EXACTLY 6 days from now. What about when the expiration date is 1-5 days from now?

(expiredate == today()) <- This will only happen when the expiration date is EXACTLY today. You should include code to check if the expiration date is negative (meaning it has already expired).
 

strollin

Well-Known Member
The problem with your code is that unless the person you are trying to notify runs the code every single day they may never see the notifications. They'll only get notified if they run the code EXACTLY 1 week before expiration, EXACTLY 6 days before expiration and EXACTLY the day of expiration. If the code is run on any other days, no notification will be given.
 
Top