SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    displays age from birthday

    Hello

    How can I display a users age from a birthday.

    if I had this data:

    born: 25/06/1990 13:57:39

    The twist is, I don't want just the year. I want the year, day, minute and second.

  2. #2
    Non-Member
    Join Date
    Jun 2012
    Posts
    88
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Do you want the age relative to the current time on the server your script is on or the users local time on their pc, bearing in mind that the time on the user's pc might be wrong.

  3. #3
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your response.

    Relative to the current time on the server

  4. #4
    Non-Member
    Join Date
    Jun 2012
    Posts
    88
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    There are lots of examples on the Internet.

    This sounds like a homework exercise, so post the code you have so far.

  5. #5
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, this isn't homework. This is what I have so far, which only gives me the year.

    PHP Code:
    function GetAge($Birthdate)
    {
            
    // Explode the date into meaningful variables
           
    $Dateparts explode("-"$Birthdate);
      
    // Find the differences
      
    $DayDiff =  $Dateparts[0] -  date("d");
           
    $MonthDiff date("m") - $Dateparts[1];
          
           
    $YearDiff date("Y") -$Dateparts[2];
       
    // If the birthday has not occurred this year
    if ($DayDiff or $MonthDiff 0)  {    $YearDiff--;  }

    echo 
    $YearDiff ;
      
    }
    GetAge("25-06-1990"); 

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Any clues here? birthday countdown in PHP/JS.

    It seems you are getting the DOB from an sql table, is that so?

  7. #7
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Are you on PHP >= 5.3?

    If so, you can use DateTime and DateInterval

    PHP Code:
    $now = new \DateTime();
    $then = new \DateTime($dob_from_database);
    $interval $now->diff($then); // interval is a \DateInterval object
    echo $interval->format('%y year(s), %m month(s), %d day(s), %i minute(s) and %s second(s)'); 
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


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
  •