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.";
}
?>
You can use the days property of your $difference object:
if ($difference->days <= (18 * 30) && $difference->days >= (6 * 30) {
echo "Green warning: At least 6 months left.";
}
About property days in Php docs:
If the DateInterval object was created by DateTime::diff(), then this is the total number of days between the start and end dates. Otherwise, days will be FALSE.
http://php.net/manual/en/class.dateinterval.php
EDIT
If you need 6 and 18 months exactly you could use this approach:
$targetDate = new DateTime('20170529'); //here your target date;
$now = new DateTime(); //by default today;
$min = clone $targetDate;
$min->add(new DateInterval('P6M'));
$max = clone $targetDate;
$max->add(new DateInterval('P18M'));
if($now >= $min && $now <= $max){
// your code here
}
You could use a date-diff(..) function to retrieve numbers of months between two date objects
<?php
// Set first exam date
$firstexamdate = "2015-08-20";
// Work out date that is 18 months from first exam date
$nextDate = new DateTime($firstexamdate);
$nextDate->add(new DateInterval('P18M'));
// Calculate nr of months
$today = new DateTime();
$interval = date_diff($today, $nextDate);
$months = $interval->y * 12 + $interval->m;
// Output correct warning color
if ($months < 18 and $months>= 6) {
echo "Green warning: At least 6 months left.";
} elseif ($months < 6 and $months>=1) {
echo "Orange warning: Less than 5 months left.";
} elseif ($months < 1) {
echo "Red warning: Less than one month left.";
}
?>