Jump to content

Comparing Dates: timestamps


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
https://forums.phpfreaks.com/topic/288298-comparing-dates-timestamps/
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));

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.';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.