kevinkhan Posted January 31, 2011 Share Posted January 31, 2011 I am scraping Birthdays from a webpage that are in this format September 15, 1987 $age = 15; $page = source code of a html page; so far i have made this function How do i return the function as true if the date scaped makes the person over 15 and false if the person is younger? function checkForAge($page,$age) { preg_match('|Born on ([a-zA-Z]*\s[0-9]*,\s[0-9]*)\\\u003c\\\/span>|', $page, $match); if($match && count($match)>0) { echo "Match Found"; $dateOfBirth = str_replace(",","",$match[1]); // $dateOfBirth = date('d/m/Y', strtotime($dateOfBirth)); $dateOfBirth = strtotime($dateOfBirth); /* if("date of birth makes person over $age) { return true; } else { return false; } */ } Quote Link to comment https://forums.phpfreaks.com/topic/226250-need-help-with-a-date-function/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 31, 2011 Share Posted January 31, 2011 If you get your date of birth into a YYYY-MM-DD format, the following will calculate a person's age in whole years - $today = date("Y-m-d"); $age = substr($today, 0, 4) - substr($dob, 0, 4) - (substr($today, 5,5) < substr($dob, 5,5)); // difference in years, subtract one if the birthday has not occurred yet in the current year Quote Link to comment https://forums.phpfreaks.com/topic/226250-need-help-with-a-date-function/#findComment-1167933 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.