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 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Age calculation function

    Code:
    PHP Code:
    function age($month$day$year) {  (checkdate($month$day$year)==0) ? die("no such date.") : "";  $y=gmstrftime("%Y");  $m=gmstrftime("%m");  $d=gmstrftime("%d");  $age=$y-$year;  return (($m<=$month)&&($d<=$day)) ? $age-$age; } 
    the code above takes month, date, and year of birth as arguments, check if they exist/are valid, if yes it continues, take current year, month, and date...does some calculations and reasoning, and returns the age...

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,026
    Thanks
    2
    Thanked 315 Times in 307 Posts
    If you were to edit that post so that it is formatted so that it is readable, it would help. There is also no reason for the [ code][ /code] tags in addition to the [ php][ /php] tags, that just makes it more unreadable.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    PHP Code:
    <?php

    function age($month$day$year)
    {
        (
    checkdate($month$day$year) == 0) ? die("no such date.") : "";
        
    $y gmstrftime("%Y");
        
    $m gmstrftime("%m");
        
    $d gmstrftime("%d");
        
    $age $y $year;
        return ((
    $m <= $month) && ($d <= $day)) ? $age $age;
    }


    ?>

  • #4
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Close but no cigar

    Quote Originally Posted by Zangeel View Post
    PHP Code:
    <?php

    function age($month$day$year)
    {
        (
    checkdate($month$day$year) == 0) ? die("no such date.") : "";
        
    $y gmstrftime("%Y");
        
    $m gmstrftime("%m");
        
    $d gmstrftime("%d");
        
    $age $y $year;
        return ((
    $m <= $month) && ($d <= $day)) ? $age $age;
    }


    ?>
    Didn't quite work... wasn't detecting the right age. Here is my modified code that seems to be working fine:

    PHP Code:
    function age($month$day$year)
    {
        (
    checkdate($month$day$year) == 0) ? die("no such date.") : "";
        
    $y gmstrftime("%Y");
        
    $m gmstrftime("%m");
        
    $d gmstrftime("%d");
        
    $age $y $year;
        if(
    $m <= $month)
        {
            if(
    $m == $month)
            {
                if(
    $d $day$age $age 1;
            }
            else 
    $age $age 1;
        }
        return(
    $age);


  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    I wouldn't recommend hijacking the output from within the function using exit(), die(), etc... Just grab the data and control the output from the `view` scope:
    PHP Code:
    function age$month$day$year )
    {
        if ( 
    checkdate$month$day$year ) )
        {
            
    //calculate $age as an integer
            
    return age;
        }
        return 
    FALSE;

    Example:
    PHP Code:
    $age age12251990 );

    if ( 
    FALSE === $age )
    {
        echo 
    'Not a valid birth date';
    }
    else
    {
        echo 
    'Your age: ' $age;

    Last edited by kbluhm; 04-14-2009 at 02:08 AM.


  •  

    Posting Permissions

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