LinkShare  Referral  Program

New

Advertise ad Here
learn more
            PHP > date manipulation functions>age finder
Added on   06.09.09
Clicks     2102
Rating:   5.00   out of 5 from   3   raters      
         rate this page   
 Not rated yet.Be the first one to vote!!

iBuyOfficesupply.com Inc. There is not Comments for this Article,Be the first one
Ask any relevant question in the Comments or leave your valuable feed-back

printer  Print version of this article

star  Add it to your favourite!

mail email this page to a frend
LinkShare  Referral  Program

Date and time manipulation functions



 Find anybody's age function




 

 

    If you need to find the age of somebody with the date of birth,here is a simple function for that.   .



    We assume that all the functions will be saved in a file called php_functions.php

    <?php
    function age($date_of_birth){
    $dob =date('Y-m-d', strtotime($date_of_birth));
    $age = date('Y') - date('Y', strtotime($dob));
    if (date('md') < date('md', strtotime($dob))) {
    $age--;
    }
    return $age;
    }
    ?>

    Explaining the above function is quite simple, as you can see ,it has only 1 parameter
    $date_of_birth  is where you enter the date of birth of the person.It must be in this format: d-mm-yyyy  or mm/d/yyyy

    $dob =date('Y-m-d', strtotime($date_of_birth));This line of code gets the given date in the correct format

    $age = date('Y') - date('Y', strtotime($dob));$age will hold the result of the difference between today's year and the year of DOB

    if (date('md') < date('md', strtotime($dob))) {
    $age--;
    } if today's day is not your birthday yet, this line of code will subtract 1 year from your age.

    See the example below to use the function

    <?php
    include 'php_functions.php';

    $age=age('2-09-1950');


    echo $age
    //if today's date is 10 Sep 2009 will output 58

    // if today's date is 22 Sep 2009 will output 59

    ?>

     

    Try the example below

     Your date of birth
    d-mm-yyyy or mm/d/yyyy
     
     Your age is