Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Location
    In the wilderness
    Posts
    106
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Date of birth function

    Here is a snippet for quickly calculating and displaying how old a person is, which will update itself each year

    PHP Code:
    <?php
    function dateDiff($dformat$endDate$beginDate)
    {
    $date_parts1=explode($dformat$beginDate);
    $date_parts2=explode($dformat$endDate);
    $start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
    $end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
    return 
    $end_date $start_date;
    }
    //Enter date of birth below in MM/DD/YYYY
    $dob="11/12/1980";
    echo 
    round(dateDiff("/"date("m/d/Y"time()), $dob)/3650) . " years.";
    ?>
    Example output:

    26 Years

  • #2
    Regular Coder xconspirisist's Avatar
    Join Date
    Jun 2006
    Location
    Great Britain.
    Posts
    138
    Thanks
    1
    Thanked 6 Times in 6 Posts
    That's rather a long and bloated way of doing things. Why not do something like this?

    PHP Code:
    /**
     * Takes a standard string describing a date, and calculates how many years 
     * have passed between now and then. In this purpose, used to calculate how
     * old somebody is.
     *
     * @author xconspirisist <xconspirisist@gmail.com>
     */
    function yearsOld($dob) {
        
    $dob strtotime($dob);

        if (
    $dob === false) {
            echo 
    'Your supplied date could not be parsed by strtotime()';
            return;
        }

        
    $elapsedSeconds mktime() - $dob;
        
        
    // There are 31556926 seconds in a year.
        
    $yearsOld $elapsedSeconds 31556926;
        
    $yearsOld floor($yearsOld);

        return 
    $yearsOld;

    }

    echo 
    'You are ' yearsOld('20th August 1987') . ' years old.'
    If I have been helpful, use the "thank" button - It makes me happy!

    xconspirisist.co.uk - homepage of my online alias
    technowax.net - a community for people interested in all forms of modern technology.

  • #3
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    There are 31556926 seconds in a year
    are you basing this on 365.27 days? are you accounting for leap years? or are you just going for ballpark figures?

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    365.27 is wrong (and has no relationship to the number of seconds specified earlier) there are 365.242803 days in a year on average which is 31556977 seconds. A few seconds probably doesn't make any significant difference to the calculation given that you are working on an average year length and so it needs to round up or down to a whole day.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    You can let strtotime (correctly) account for leap years if it worries you:

    PHP Code:
    function years_old($dob)
    {
        if ((
    $dob strtotime($dob)) === false)
        {
            throw new 
    Exception('strtotime parsing fails it');
        }
        for (
    $i 0strtotime("-$i year") > $dob; ++$i);
        return 
    $i 1;

    You're far more likely to encounter problems from timezone discrepancies and from people not entering the hour and minute of their birth than you are from losing seconds over the years, though. At least for users who aren't several thousand years old.
    Last edited by ralph l mayo; 01-08-2007 at 09:14 PM.

  • #6
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    Quote Originally Posted by felgall View Post
    365.27 is wrong (and has no relationship to the number of seconds specified earlier) there are 365.242803 days in a year on average which is 31556977 seconds. A few seconds probably doesn't make any significant difference to the calculation given that you are working on an average year length and so it needs to round up or down to a whole day.
    thanks, yes I realize that my number was wrong, I didn't look it up and had remembered it incorrectly. I didn't know if the fraction of days had been taken into account, that is all.

  • #7
    Regular Coder xconspirisist's Avatar
    Join Date
    Jun 2006
    Location
    Great Britain.
    Posts
    138
    Thanks
    1
    Thanked 6 Times in 6 Posts
    I would of hoped from my description, that I was posting rather basic code, but still - those are interesting points. Thanks for your advice.
    If I have been helpful, use the "thank" button - It makes me happy!

    xconspirisist.co.uk - homepage of my online alias
    technowax.net - a community for people interested in all forms of modern technology.

  • #8
    New to the CF scene
    Join Date
    May 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Great but no

    It's an interesting little program but i personally argue whit time. I was thinking giving up clocks but this is almost impossible. I don't like to measure the time passing, i want to ignore it as much as i can.


  •  

    Posting Permissions

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