I have been having some issues with a date compare I have been getting a date from a database in YYYY-MM-DD format, and trying to compare todays date to that date to see if it has expired, then also a 30 day from the YYYY-MM-DD to check if it is nearing expiration
and finally a 60 day from the YYYY-MM-DD to check if it is expiring.
I am using a global flag to figure out which one it is then I am going to have it email if they are coming close to expiration. The issue I keep coming against is that it is flagging even if the dates aren't accurate, so I think I have a problem with my compares.
//Include the neccessary DB connection
include_once("connect.php");
doDb();
global $dateFlag;
global $dateNow;
$dateFlag = 0;
$dateNow = date('Y-m-d');
//Get the data for checking the database for expired documents for users.
//Will need the exp_date, start_date user and type and doc_id to find the user
$query_all = "SELECT exp_date, start_date, type, doc_id FROM user_doc";
$result = mysqli_query($mysqli,$query_all) or die("MySQL error: " . mysqli_error($mysqli) . "<hr>\nQuery: $query_all");
while($row = mysqli_fetch_array($result, MYSQLI_NUM))
{
//get the non-tested values and assign them
$type = $row[2];
$doc_id = $row[3];
//first we need to check if the dates are for drug tests or for certifications. the type will be row[2]
if($row[2] != 'drug_1' && $row[2] != 'drug_2' && $row[2] != 'drug_3'){
//we have to test the exp_date first since we have the exp_date we can test the expiration first.
//get the exp_date
$date = $row[0];
//setup 30 day compare
$thirtyDate = strtotime('-30 days', strtotime( $date ) );
$thirtyDate = date('Y-m-d' , $thirtyDate);
//setup 60 day compare
$sixtyDate = strtotime('-60 days', strtotime( $date ) );
$sixtyDate = date('Y-m-d' , $sixtyDate);
if($date < date('Y-m-d')){
//assign a value to dateFlag
$dateFlag = 1;
}
//next we test if it is less than 60 days
if($sixtyDate <= $dateNow){
//assign a value to dateFlag
$dateFlag = 3;
}
//next we test if it is less than 30 days
if($thirtyDate <= $dateNow){
//assign a value to dateFlag
$dateFlag = 2;
}
}