I have some code that allows users to input the date of their 'first exam', compares that date to today's date and displays a warning/notification if the difference between those two dates is a certain amount (as the rest of their exams must be completed within 18 months of their first exam being taken). I'm having trouble working out how to compare the dates/difference between the dates in the conditional statement - in other words, how can I compare $difference
to "18 months" or "2 months", etc. If anyone could point me in the right direction it would be greatly appreciated.
Code:
<?php
// Set first exam date
$firstexamdate = "2015-08-20";
// Work out date that is 18 months from first exam date
$add_18_months = strtotime($firstexamdate . ' + 18 months');
$eighteen_months_time = date('Y-m-d',$add_18_months);
// Check
echo "Date of first exam: " . $firstexamdate . "<br>";
echo "18 months from this date: " . $eighteen_months_time . "<br>";
// Work out diff between cut-off date and today
$today = date('Y-m-d');
$date1 = new DateTime($eighteen_months_time);
$date2 = new DateTime($today);
$difference = $date1->diff($date2);
// Display
echo "Difference/time remaining: " . $difference->y . " years, " . $difference->m . " months, " . $difference->d . " days " . "<br";
// BELOW CODE NOT WORKING
// Display $difference as string
echo $difference->format('Y-m-d');
// Output correct warning colour
if ($difference <= /* 18 months */ and >= /* 6 months */ {
echo "Green warning: At least 6 months left.";
} elseif ($difference <= /* 5 months */ and >= /* 2 months */ {
echo "Orange warning: Less than 5 months left.";
} elseif ($difference <= /* 1 months */ and >= /* 0 months */ {
echo "Red warning: Less than one month left.";
}
?>