Majestic1985 Posted November 10, 2016 Share Posted November 10, 2016 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/1985Today's Date is 11/9/2016Output (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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/302504-simple-php-program-will-not-return-correct-date-values/ Share on other sites More sharing options...
requinix Posted November 10, 2016 Share Posted November 10, 2016 Hint: 153 + 212 = 365 And clarification: don't do subtraction to fix it (that's not quite accurate) but think about what "direction" the difference uses. Quote Link to comment https://forums.phpfreaks.com/topic/302504-simple-php-program-will-not-return-correct-date-values/#findComment-1539169 Share on other sites More sharing options...
Majestic1985 Posted November 10, 2016 Author Share Posted November 10, 2016 (edited) Hint: 153 + 212 = 365 And clarification: don't do subtraction to fix it (that's not quite accurate) but think about what "direction" the difference uses I thought I made the correct changes, but I'm still getting an incorrect number. Your hint helped me realize what may be wrong, but I think I've been working on this problem for so long, that I can't see the answer right in front of me. Could you elaborate a bit more. Thanks again. Edited November 10, 2016 by Majestic1985 Quote Link to comment https://forums.phpfreaks.com/topic/302504-simple-php-program-will-not-return-correct-date-values/#findComment-1539170 Share on other sites More sharing options...
benanamen Posted November 10, 2016 Share Posted November 10, 2016 Perhaps you should refer to the manual. You have buried yourself in so much code you can't think straight. http://php.net/manual/en/datetime.diff.php <?php $datetime1 = new DateTime('2009-10-11'); $datetime2 = new DateTime('2009-10-13'); $interval = $datetime1->diff($datetime2); echo $interval->format('%R%a days'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/302504-simple-php-program-will-not-return-correct-date-values/#findComment-1539171 Share on other sites More sharing options...
requinix Posted November 10, 2016 Share Posted November 10, 2016 There was a specific part of the docs I wanted you to see, but apparently it's in a user comment and not part of the official documentation: It is worth noting, IMO, and it is implied in the docs but not explicitly stated, that the object on which diff is called is subtracted from the object that is passed to diff. i.e. $now->diff($tomorrow) is positive. Quote Link to comment https://forums.phpfreaks.com/topic/302504-simple-php-program-will-not-return-correct-date-values/#findComment-1539173 Share on other sites More sharing options...
Jacques1 Posted November 10, 2016 Share Posted November 10, 2016 The approach is weird, to say the least. First you check if the person was born before today (shouldn't that almost always be the case?). Then you travel back into the year of the birth and calculate the difference between that past date and the birth date. What is this supposed to tell you? The difference can be negative. It can also be one day off compared to the actual difference in this year due to leap years. Long story short, it's not very useful. When you want to calculate the days to your next birthday, you care about this year and possibly the next year. Not 1985. There are two cases: Your birth day in this year (i. e. 2016-04-11) is either today or after today. Then you simply calculate the difference between that day and today. The birth day in this year was before today. Then you calculate the difference between the birth day in the next year (2017-04-11) and today. This yields 153 days. I strongly recommend you always solve the problem on paper before typing anything on the keyboard. When you simultaneously struggle with the programming language and the problem itself, you're unlikely to get the correct result. You also need to check your understanding of basic control structures. The bizarre switch-statement-acting-as-an-if-statement is not really how PHP (or any language) works. Quote Link to comment https://forums.phpfreaks.com/topic/302504-simple-php-program-will-not-return-correct-date-values/#findComment-1539180 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.