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;
		}
		
				
	}

as an update, if I remove the flags for the 30 and sixty all of them set the dateFlag to 1 so my error is with all of the date compares.

Alright I went ahead ans solved my own problem. looks like I was forgetting to reset $dateFlag Also to anyone who wants to know I was doing the date compare wrong. I have included the final result, hopefully it will be useful to someone.

doDb();
	global $dateFlag;
	global $dateNow;
	
	$dateNow = strtotime(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];
	$dateFlag = 0;
	//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 = strtotime($row[0]);
		//setup 30 day compare
		$thirtyDate = strtotime('-30 days', $date  );
		
		//setup 60 day compare
		$sixtyDate = strtotime('-60 days', $date  );
		
		if($date < $dateNow){
			//assign a value to dateFlag 
			$dateFlag = 1;
		}else
		{
			$dateFlag = 0;
		}
		//next we test if it is less than 60 days
		if($sixtyDate < $dateNow && $dateFlag != 1 ){
			//assign a value to dateFlag 
			$dateFlag = 3;
		}
		//next we test if it is less than 30 days
		if($thirtyDate < $dateNow && $dateFlag != 1 ){
			//assign a value to dateFlag 
			$dateFlag = 2;
		}
		
				
	}

The dateFlag extras are gratuitous like the else dateFlag = 0; Which is what led me to figure out my logical error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.