Jump to content

Simple PHP program will not return correct date values


Majestic1985

Recommended Posts

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






?>
Link to comment
Share on other sites

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 by Majestic1985
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.