Hello All,
I'm a student currently enrolled in a PHP course, and our instructor has told us to create code that would accept a users birthday and then calculate how many days from today is their next birthday. The program returns a value, but the value is incorrect. I should never receive a value over 365 days.
Example: Input: 04/11/1985 Today's Date is 11/9/2016 Output (should be): "You have 153 days until your next Birthday"
Actual Output: "You have 212 days until your next Birthday"
What did I do wrong?
Thanks in advance for any assistance
Michelle
<?php
$johnsBirthday = $_GET ['JohnBday'];
$jakesBirthday = $_GET ['JakeBday'];
$john_bday = new DateTime($_GET['JohnBday']);
$jake_bday = new DateTime($_GET['JakeBday']);
$today_date = new DateTime();
switch (true) {
case ($john_bday < $today_date) :
$today_date->setDate($john_bday->format('Y'), $today_date->format('m'), $today_date->format('d'));
break;
case ($today_date < $john_bday) :
$john_bday->setDate($today_date->format('Y'), $john_bday->format('m'), $john_bday->format('d'));
break;
}
switch (true) {
case ($today_date < $jake_bday) :
$jake_bday->setDate($today_date->format('Y'), $jake_bday->format('m'), $jake_bday->format('d'));
break;
case ($jake_bday < $today_date) :
$jake_bday->setDate($today_date->format('Y'), $jake_bday->format('m'), $jake_bday->format('d'));
break;
}
$john_interval = $john_bday->diff($today_date);
$john_diff = $john_interval->format('%a');
echo "John you have $john_diff days until your next Birthday</br>";
$jake_interval = $jake_bday->diff($today_date);
$jake_diff = $jake_interval->format('%a');
echo "Jake you have $jake_diff days until your next Birthday</br>";
if ($johnsBirthday < $jakesBirthday)
{
echo "John is older than Jake</br>";
}
elseif ($johnsBirthday > $jakesBirthday)
{
echo "Jake is older than John</br>";
}
else
{
echo "Jake and John are twins";
}
?>