Solved

I need to determine  if someones age birthday is within 120 days

Posted on 2014-09-17
9
17 Views
Last Modified: 2016-05-23
I have to determin if a person is eligible based on them turning 65. I need to know if they are with in a window of 120 days from the first day of the month of them turning 65. This window is 120 days in the future or 120 days in the past from the first day of the month of their 65th birthday.

I am not sure how to come up with this. Here is what I have so far:

$bday = new DateTime('2.12.1945');

$today = new DateTime('2010-08-01 00:00:00'); // for testing purposes

$diff = $today->diff($bday);

printf('%d years, %d month, %d days', $diff->y, $diff->m, $diff->d);

Open in new window


I am a bit lost so far.
0
Comment
Question by:semfreak
  • 5
  • 2
9 Comments
 
LVL 29

Accepted Solution

by:
becraig earned 250 total points
ID: 40328242
<?php

     $now = time(); // or your date as well
     $your_date = strtotime("2010-01-01");
     $datediff = $now - $your_date;
     echo floor($datediff/(60*60*24));

?>

Open in new window


This will give you the difference in days.

$datediff/60*60*24
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40328255
This and many other date/time concepts are documented here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Beware of doing arithmetic with date and time values.  Leap year and daylight savings time are a couple of things that can trip you up.  Whenever possible use the built-in PHP functions.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40328313
This should help you get started.
http://iconoun.com/demo/temp_semfreak.php

<?php // demo/temp_semfreak.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28520303.html
// This window is 120 days in the future
// or 120 days in the past from the first day
// of the month of their 65th birthday.

// USE THE URL GET ARGUMENT "d=" TO PROVIDE A BIRTHDATE
$d = !empty($_GET['d']) ? strtotime($_GET['d']) : 0;
if ($d)
{
    $alpha = date('Y-m-d', strtotime('TODAY - 120 days'));
    $omega = date('Y-m-d', strtotime('TODAY + 120 days'));
    $birth = date('Y-m-d', strtotime($_GET['d'] . ' + 65 years'));

    if ($birth < $alpha)
    {
        echo "BIRTHDAY " . $_GET['d'] . " HAS PASSED THE WINDOW OF ELIGIBILITY";
    }
    elseif ($birth > $omega)
    {
        echo "BIRTHDAY " . $_GET['d'] . " IS NOT ELIGIBLE YET";
    }
    else
    {
        echo "BIRTHDAY " . $_GET['d'] . " IS CURRENTLY ELIGIBLE";
    }
}

// CREATE THE FORM FOR THE INPUT BIRTHDAY
$form = <<<EOD
<form>
Birthday?
<input name="d" />
<input type="submit" value="Check" />
</form>
EOD;
echo $form;

Open in new window

0
 

Author Comment

by:semfreak
ID: 40328315
Here is the revised code I am using to get me there:

// Example birthday
$dob = "2-12-1945";

function threeMonthsOut($birthdate) {

    return date("Y-m-d", strtotime("+3 months", strtotime($birthdate)));
}

function threeMonthsBack($birthdate) {

    return date("Y-m-d", strtotime("-3 months", strtotime($birthdate)));
}


//are you in your 65 year 120 + -  window?
$windowforward = threeMonthsOut($dob);
$windowBack = threeMonthsBack($dob);
$today = date("Y-m-d");

// YES or NO?

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40328322
I understood the window to be 120 days.  That is not the same as 3 months.  The term "month" is ambiguous, but in any case, three of them will never give you 120 days.
0
 

Author Comment

by:semfreak
ID: 40328370
What Am I doing wrong. I am trying to pass $d with out using the form and I am not getting anytging back from $d:

$d = strtotime(1944-7-22);
echo $d;
//$d = !empty($_GET['d']) ? strtotime($_GET['d']) : 0;
if ($d)
{
    $alpha = date('Y-m-d', strtotime('TODAY - 120 days'));
    $omega = date('Y-m-d', strtotime('TODAY + 120 days'));
    $birth = date('Y-m-d', strtotime($_GET['d'] . ' + 65 years'));

    if ($birth < $alpha)
    {
        echo "BIRTHDAY " . $_GET['d'] . " HAS PASSED THE WINDOW OF ELIGIBILITY";
    }
    elseif ($birth > $omega)
    {
        echo "BIRTHDAY " . $_GET['d'] . " IS NOT ELIGIBLE YET";
    }
    else
    {
        echo "BIRTHDAY " . $_GET['d'] . " IS CURRENTLY ELIGIBLE";
    }
}

?>

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 40328471
From the look of that last block of code, it seems like you might be new to PHP.  This article has some good "getting started" resources.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

Try it like this:
<?php // demo/temp_semfreak.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28520303.html
// This window is 120 days in the future
// or 120 days in the past from the first day
// of the month of their 65th birthday.

// OVERWRITE THE $d VARIABLE
$d = '1944-7-22';
if ($d)
{
    $alpha = date('Y-m-d', strtotime('TODAY - 120 days'));
    $omega = date('Y-m-d', strtotime('TODAY + 120 days'));
    $birth = date('Y-m-d', strtotime($d . ' + 65 years'));

    if ($birth < $alpha)
    {
        echo "BIRTHDAY " . $d . " HAS PASSED THE WINDOW OF ELIGIBILITY";
    }
    elseif ($birth > $omega)
    {
        echo "BIRTHDAY " . $d . " IS NOT ELIGIBLE YET";
    }
    else
    {
        echo "BIRTHDAY " . $d . " IS CURRENTLY ELIGIBLE";
    }
}

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41610918
The correct answer with a tested-and-working code example is at #40328313
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
change password links 7 73
Form Processing in PHP 11 31
How to set a countdown by not using the PC time in PHP 36 44
How to convert my query to the proper format? 5 17
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now