Jump to content

Comparing Dates: timestamps


BuildMyWeb
Go to solution Solved by BuildMyWeb,

Recommended Posts

so i have been asked to verify that someone is 21 according to the dob entered in an HTML FRI form.  basically we are accepting data in text input fields.  two digits for month, two for day, and 4 for year.  the data is then formatted as per the following:  "mm/dd/yy" via concatenation.

 

in our handler script, we are doing the following:

$dob         = $arr_m[$i] . "/" . $arr_d[$i] . "/" . $arr_y[$i];
            $dob_ts     = strtotime($dob);
            $today_ts    = time();
            $twentyone    = 21*365*24*60*60;
            
            if( ($today_ts - $twentyone) > $dob_ts )
            echo"we are 21";

our results are not what we expect tho.  today's date is may 6 2014.  however our script is accepting 05/11/1993 as being 21 years old and not 05/12/1993.  we are off by a few days. i assume there is a better way to calculate 21 years of age from today's date?

 

 

Link to comment
Share on other sites

the age of something is the difference in years between the current year and the year of birth, subtract one if the birthday (month/day) hasn't occurred yet in the current year.

 

an example -

$dob = '1993-05-12'; // standard yyyy-mm-dd format
$today = date("Y-m-d");
$age = substr($today, 0, 4) - substr($dob, 0, 4) - (substr($today, 5,5) < substr($dob, 5,5));
Link to comment
Share on other sites

Using the date functions of PHP isn't challenging enough for you guys, I guess?  ::)

<?php

// check the manual for the acceptable date formats
$birthdate = '1993-05-06';

if (strtotime($birthdate) <= strtotime('21 years ago'))
{
	echo 'You are indeed 21.';
}
else
{
	echo 'You are not old enough.';
}
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.