SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Tidy Date-of-birth to Age conversion code

    Hello,

    DoB to years:

    <?
    //calculate years of age (input string: $DateOfBirth = YYYY-MM-DD)

    $age = strtotime(date("Y-m-d")) - strtotime($DateOfBirth);
    $age = round( $age/31556926);
    echo $age,"<br />";
    ?>

    i have updated it to account for the fact that an average year has 365.242199 days in it, with a result of 18 hours more accuracy per century.

    it is accurate to 1 second every 100 years, although it will be off by half a day on every birthday due to the gap year being every 4th year. it is more accurate for celestial time of your birthday to the second. it depends if you code by celestial or earth time.

    -=A VERY BORING CALCULATION EXPLANATION FOLLOWS TO EXPLAIN THE FEW SECONDS BETTER ACCURACY!=-

    the long number is the number of seconds in a year (60x60x24x365.25)
    365.25 days per year is 31557600
    365.242199 days per year is 31556925.995

    So in case your code is still running in 100 years, you would have about 18 hours more accuracy, or 11 minutes per year.

    Sorry i am enjoying being a dork once again! i love this programming stuff i havent done any coding since i learnt dragon naturally speaking scripts 5 years ago! lots of catching up to do in geekiness here!

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,328
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    accurate to within 1 second? sorry, no

    plug in a birthdate of 2008-12-07 and see what you get
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    To get the age accurately you need to subtract the year of birth from the current year and then if the current month-day is not less than the birth month-day then add one to the result.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Or you could use the DateTime/DateInterval classes

    PHP Code:
    echo DateTime::createFromFormat("!d/m/Y""06/12/1968")
         ->
    diff(new DateTime("today"))
         ->
    format("%y years old"); 
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function age($date_formatted){
            
    $iTimestamp =  strtotime($date_formatted);
            
            
    // See http://php.net/date for what the first arguments mean.
            
    $iDiffYear  date('Y') - date('Y'$iTimestamp);
            
    $iDiffMonth date('n') - date('n'$iTimestamp);
            
    $iDiffDay   date('j') - date('j'$iTimestamp);
            
            
    // If birthday has not happen yet for this year, subtract 1.
            
    if ($iDiffMonth || ($iDiffMonth == && $iDiffDay 0))
            {
                
    $iDiffYear--;
            }
                
            return 
    $iDiffYear;
        } 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •