Jump to content

Need help with a date function


kevinkhan

Recommended Posts

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;
         }
*/

    }

Link to comment
https://forums.phpfreaks.com/topic/226250-need-help-with-a-date-function/
Share on other sites

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

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.