SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Wadge! F4nat1c's Avatar
    Join Date
    Oct 2005
    Location
    South Wales, UK
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calculating an age function.

    Just to contribute to the growth of PHP and all that, I've made a script that can calculate the age of someone based on what day, month(in text format) and year they submit.

    The function gets the values from the drop down boxes (or input boxes if you wish) and then calculates what month they submitted by changing the month into a numerical value (E.g. April becomes 04).

    The Function - This can go at the top of the script or whatever

    PHP Code:
    function getAgeByDate($iMonth$iDay$iYear) {
        
    $iTimeStamp = (mktime() - 86400) - mktime(000$iMonth$iDay$iYear);
        
    $iDays $iTimeStamp 86400;
        
    $iYears floor($iDays 365.25);
        return 
    $iYears;

    }

    //converts a text month to its corresponding number value
    function monthtonumber($month) {
        switch (
    $month) {
        case 
    "January":
        
    $nomonth "01";
        break;
        case 
    "February":
        
    $nomonth "02";
        break;
        case 
    "March":
        
    $nomonth "03";
        break;
        case 
    "April":
        
    $nomonth "04";
        break;
        case 
    "May":
        
    $nomonth "05";
        break;
        case 
    "June":
        
    $nomonth "06";
        break;
        case 
    "July":
        
    $nomonth "07";
        break;
        case 
    "August":
        
    $nomonth "08";
        break;
        case 
    "September":
        
    $nomonth "09";
        break;
        case 
    "October":
        
    $nomonth "10";
        break;
        case 
    "November":
        
    $nomonth "11";
        break;
        case 
    "December":
        
    $nomonth "12";
        break;
    }
    return 
    $nomonth;

    And to get the value of the age, call it using this:

    PHP Code:
    $birthmonth monthtonumber($birthmonth);

    $age getAgeByDate($birthmonth$birthday$birthyear); 
    You could say I made this out of boredum, but I thought i'd share it for future people needing something like this.
    OMFG SitePoint ROXORZ TEH BIG ONE111!
    Wish you were invisible?

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    Thanks James, good to see I'm not the only bored programmer out there
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Addict lmasi02's Avatar
    Join Date
    Aug 2004
    Location
    Zambia
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    round function would wrk better than floor
    Power of Knowledge


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
  •