/    Sign up×
Community /Pin to ProfileBookmark

PHP Date and Time Handling

I’m real bad at using the PHP date and time functions. I was hoping someone could tell me how I could check to see if someone is 18 by passing in a date of birth to a function and obviously comparing it to today’s date.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@DexterMorganJan 02.2011 — Is this what you mean?
[code=php]
<?php
/**
* Check if a given D.O.B is over 18
* @param string date of birth
* @return bool
*/
function checkAge($dob){
$age = strtotime($dob);
$eighteen = 566092800; // 18 years

$diff = time() - $age;

if($diff > $eighteen){
return true;
}
return false;

}

//usage
if(checkAge('11 may 2000')){
echo 'Old enough';
}else{
echo 'not Old enough';
}
?>

[/code]
Copy linkTweet thisAlerts:
@NogDogJan 03.2011 — If using PHP 5.3.0+, you can use [url=http://www.php.net/manual/en/datetime.diff.php]DateTime::diff[/url](). Also, if working with database date information, it might be desirable to do it as part of the DB query, for instance using the MySQL [url=http://www.php.net/manual/en/datetime.diff.php]DATE_SUB() or DATE_ADD() functions[/url].
×

Success!

Help @Ntrimgs spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...