Calculate age based on date of birth










58















I have a table of users in sql and they each have birth dates. I want to convert their date of birth to their age (years only), e.g. date: 15.03.1999 age: 14 and 15.03.2014 will change to age: 15



Here I want to show the date of the user:



if(isset($_GET['id']))

$id = intval($_GET['id']);
$dnn = mysql_fetch_array($dn);
$dn = mysql_query('select username, email, skype, avatar, ' .
'date, signup_date, gender from users where id="'.$id.'"');
$dnn = mysql_fetch_array($dn);
echo "$dnn['date']";










share|improve this question
























  • possible duplicate of Calculate Age in MySQL (InnoDb)

    – John Conde
    Oct 22 '13 at 14:49











  • presumably your dates are stored using a date data type?

    – Strawberry
    Oct 22 '13 at 14:49






  • 2





    there are plenty of 'calculate age' answers out there. Google is a mighty tool! But by the way: Don't use mysql_* functions. Use PDO or MySQLi

    – Patrick Manser
    Oct 22 '13 at 14:49











  • Get current date/time and subtract the converted one from database...then convert it back to number of years, use strtotime() and date() with proper formatting.

    – bodi0
    Oct 22 '13 at 14:50












  • Isn't it amazing that no one's ever had to do this before.

    – Strawberry
    Dec 30 '13 at 11:28















58















I have a table of users in sql and they each have birth dates. I want to convert their date of birth to their age (years only), e.g. date: 15.03.1999 age: 14 and 15.03.2014 will change to age: 15



Here I want to show the date of the user:



if(isset($_GET['id']))

$id = intval($_GET['id']);
$dnn = mysql_fetch_array($dn);
$dn = mysql_query('select username, email, skype, avatar, ' .
'date, signup_date, gender from users where id="'.$id.'"');
$dnn = mysql_fetch_array($dn);
echo "$dnn['date']";










share|improve this question
























  • possible duplicate of Calculate Age in MySQL (InnoDb)

    – John Conde
    Oct 22 '13 at 14:49











  • presumably your dates are stored using a date data type?

    – Strawberry
    Oct 22 '13 at 14:49






  • 2





    there are plenty of 'calculate age' answers out there. Google is a mighty tool! But by the way: Don't use mysql_* functions. Use PDO or MySQLi

    – Patrick Manser
    Oct 22 '13 at 14:49











  • Get current date/time and subtract the converted one from database...then convert it back to number of years, use strtotime() and date() with proper formatting.

    – bodi0
    Oct 22 '13 at 14:50












  • Isn't it amazing that no one's ever had to do this before.

    – Strawberry
    Dec 30 '13 at 11:28













58












58








58


23






I have a table of users in sql and they each have birth dates. I want to convert their date of birth to their age (years only), e.g. date: 15.03.1999 age: 14 and 15.03.2014 will change to age: 15



Here I want to show the date of the user:



if(isset($_GET['id']))

$id = intval($_GET['id']);
$dnn = mysql_fetch_array($dn);
$dn = mysql_query('select username, email, skype, avatar, ' .
'date, signup_date, gender from users where id="'.$id.'"');
$dnn = mysql_fetch_array($dn);
echo "$dnn['date']";










share|improve this question
















I have a table of users in sql and they each have birth dates. I want to convert their date of birth to their age (years only), e.g. date: 15.03.1999 age: 14 and 15.03.2014 will change to age: 15



Here I want to show the date of the user:



if(isset($_GET['id']))

$id = intval($_GET['id']);
$dnn = mysql_fetch_array($dn);
$dn = mysql_query('select username, email, skype, avatar, ' .
'date, signup_date, gender from users where id="'.$id.'"');
$dnn = mysql_fetch_array($dn);
echo "$dnn['date']";







php mysql date






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 17 '17 at 3:52









Eric Leschinski

87.3k38323274




87.3k38323274










asked Oct 22 '13 at 14:47









PHPupilPHPupil

304138




304138












  • possible duplicate of Calculate Age in MySQL (InnoDb)

    – John Conde
    Oct 22 '13 at 14:49











  • presumably your dates are stored using a date data type?

    – Strawberry
    Oct 22 '13 at 14:49






  • 2





    there are plenty of 'calculate age' answers out there. Google is a mighty tool! But by the way: Don't use mysql_* functions. Use PDO or MySQLi

    – Patrick Manser
    Oct 22 '13 at 14:49











  • Get current date/time and subtract the converted one from database...then convert it back to number of years, use strtotime() and date() with proper formatting.

    – bodi0
    Oct 22 '13 at 14:50












  • Isn't it amazing that no one's ever had to do this before.

    – Strawberry
    Dec 30 '13 at 11:28

















  • possible duplicate of Calculate Age in MySQL (InnoDb)

    – John Conde
    Oct 22 '13 at 14:49











  • presumably your dates are stored using a date data type?

    – Strawberry
    Oct 22 '13 at 14:49






  • 2





    there are plenty of 'calculate age' answers out there. Google is a mighty tool! But by the way: Don't use mysql_* functions. Use PDO or MySQLi

    – Patrick Manser
    Oct 22 '13 at 14:49











  • Get current date/time and subtract the converted one from database...then convert it back to number of years, use strtotime() and date() with proper formatting.

    – bodi0
    Oct 22 '13 at 14:50












  • Isn't it amazing that no one's ever had to do this before.

    – Strawberry
    Dec 30 '13 at 11:28
















possible duplicate of Calculate Age in MySQL (InnoDb)

– John Conde
Oct 22 '13 at 14:49





possible duplicate of Calculate Age in MySQL (InnoDb)

– John Conde
Oct 22 '13 at 14:49













presumably your dates are stored using a date data type?

– Strawberry
Oct 22 '13 at 14:49





presumably your dates are stored using a date data type?

– Strawberry
Oct 22 '13 at 14:49




2




2





there are plenty of 'calculate age' answers out there. Google is a mighty tool! But by the way: Don't use mysql_* functions. Use PDO or MySQLi

– Patrick Manser
Oct 22 '13 at 14:49





there are plenty of 'calculate age' answers out there. Google is a mighty tool! But by the way: Don't use mysql_* functions. Use PDO or MySQLi

– Patrick Manser
Oct 22 '13 at 14:49













Get current date/time and subtract the converted one from database...then convert it back to number of years, use strtotime() and date() with proper formatting.

– bodi0
Oct 22 '13 at 14:50






Get current date/time and subtract the converted one from database...then convert it back to number of years, use strtotime() and date() with proper formatting.

– bodi0
Oct 22 '13 at 14:50














Isn't it amazing that no one's ever had to do this before.

– Strawberry
Dec 30 '13 at 11:28





Isn't it amazing that no one's ever had to do this before.

– Strawberry
Dec 30 '13 at 11:28












10 Answers
10






active

oldest

votes


















169














PHP >= 5.3.0



# object oriented
$from = new DateTime('1970-02-01');
$to = new DateTime('today');
echo $from->diff($to)->y;

# procedural
echo date_diff(date_create('1970-02-01'), date_create('today'))->y;


demo



functions: date_create(), date_diff()




MySQL >= 5.0.0



SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', CURDATE()) AS age


demo



functions: TIMESTAMPDIFF(), CURDATE()






share|improve this answer

























  • Fatal error: Call to undefined function date_diff() in

    – PHPupil
    Oct 22 '13 at 14:57











  • @PHPupil: date_diff is for PHP version >= 5.3.0.

    – Glavić
    Oct 22 '13 at 15:07











  • @PHPupil: upgrade PHP? ;) or use MySQL solution.

    – Glavić
    Oct 22 '13 at 15:11











  • This wouldn't work for dob's that are under 1 years old... showing 0 is not necessarily ideal for age

    – Ryman Holmes
    Apr 6 '14 at 15:14






  • 4





    @RymanHolmes: if you need to show 1 year old, instead of 0, show it. One simple if statement solves this. Where is the problem?

    – Glavić
    Apr 6 '14 at 20:01


















10














Very small code to get Age:



<?php
$dob='1981-10-07';
$diff = (date('Y') - date('Y',strtotime($dob)));
echo $diff;
?>

//output 35





share|improve this answer




















  • 6





    How did this get 11 votes? A person's age depends on the day and month of the year as well.

    – Nick Bedford
    Jun 28 '17 at 4:31











  • Your method is incorrect if person have DOB ex. 13-10-1991 and current month is July then the age is 27 which is wrong. The person age is 26 till the current month is equal to or greater then the DOB month.

    – Shaan Ansari
    Oct 1 '18 at 6:56


















4














Got this script from net (thanks to coffeecupweb)



<?php
/**
* Simple PHP age Calculator
*
* Calculate and returns age based on the date provided by the user.
* @param date of birth('Format:yyyy-mm-dd').
* @return age based on date of birth
*/
function ageCalculator($dob)
if(!empty($dob))
$birthdate = new DateTime($dob);
$today = new DateTime('today');
$age = $birthdate->diff($today)->y;
return $age;
else
return 0;


$dob = '1992-03-18';
echo ageCalculator($dob);
?>





share|improve this answer























  • works for me thanks

    – Dennis Heiden
    Feb 16 '16 at 15:15


















2














Reference Link http://www.calculator.net/age-calculator.html



$hours_in_day = 24;
$minutes_in_hour= 60;
$seconds_in_mins= 60;

$birth_date = new DateTime("1988-07-31T00:00:00");
$current_date = new DateTime();

$diff = $birth_date->diff($current_date);

echo $years = $diff->y . " years " . $diff->m . " months " . $diff->d . " day(s)"; echo "<br/>";
echo $months = ($diff->y * 12) + $diff->m . " months " . $diff->d . " day(s)"; echo "<br/>";
echo $weeks = floor($diff->days/7) . " weeks " . $diff->d%7 . " day(s)"; echo "<br/>";
echo $days = $diff->days . " days"; echo "<br/>";
echo $hours = $diff->h + ($diff->days * $hours_in_day) . " hours"; echo "<br/>";
echo $mins = $diff->h + ($diff->days * $hours_in_day * $minutes_in_hour) . " minutest"; echo "<br/>";
echo $seconds = $diff->h + ($diff->days * $hours_in_day * $minutes_in_hour * $seconds_in_mins) . " seconds"; echo "<br/>";





share|improve this answer






























    0














    For a birthday date with format Date/Month/Year



    function age($birthday)
    list($day, $month, $year) = explode("/", $birthday);
    $year_diff = date("Y") - $year;
    $month_diff = date("m") - $month;
    $day_diff = date("d") - $day;
    if ($day_diff < 0 && $month_diff==0) $year_diff--;
    if ($day_diff < 0 && $month_diff < 0) $year_diff--;
    return $year_diff;



    or the same function that accepts day, month, year as parameters :



    function age($day, $month, $year)
    $year_diff = date("Y") - $year;
    $month_diff = date("m") - $month;
    $day_diff = date("d") - $day;
    if ($day_diff < 0 && $month_diff==0) $year_diff--;
    if ($day_diff < 0 && $month_diff < 0) $year_diff--;
    return $year_diff;



    You can use it like this :



    echo age("20/01/2000");


    which will output the correct age (On 4 June, it's 14).






    share|improve this answer
































      0














       $dob = $this->dateOfBirth; //Datetime 
      $currentDate = new DateTime();
      $dateDiff = $dob->diff($currentDate);
      $years = $dateDiff->y;
      $months = $dateDiff->m;
      $days = $dateDiff->d;
      $age = $years .' Year(s)';

      if($years === 0)
      $age = $months .' Month(s)';
      if($months === 0)
      $age = $days .' Day(s)';


      return $age;





      share|improve this answer






























        0














        declare @dateOfBirth date
        select @dateOfBirth = '2000-01-01'



        SELECT datediff(YEAR,@dateOfBirth,getdate()) as Age






        share|improve this answer
































          0














          To Calculate age from Date of birth.



          $dob = '1991-09-30';
          (((int) date("m",strtotime($dob)) >= (int) date('m')) && ((int) date("d",strtotime($dob)) >= (int) date('d')))

          ?
          $age = (date('Y') - date('Y',strtotime($dob)))
          :
          $age = (date('Y') - date('Y',strtotime($dob)))-1;


          OUTPUT: 26






          share|improve this answer
































            0














            I hope you will find this useful.



            $query1="SELECT TIMESTAMPDIFF (YEAR, YOUR_DOB_COLUMN, CURDATE()) AS age FROM your_table WHERE id='$user_id'";
            $res1=mysql_query($query1);
            $row=mysql_fetch_array($res1);
            echo $row['age'];





            share|improve this answer
































              -1














              There is a simple way to find the date from any birthdate by using substr of PHP



              $birth_date = '15.03.2014';
              $date = substr($birth_date, 0, 2);
              echo $date;


              Which will just simply give you the output date of that birth date.



              In this case, that will be 15.



              See substr of PHP for more...






              share|improve this answer























              • The question asks for the age, not the 2-digit year someone was born in.

                – Martijn Pieters
                Oct 1 '18 at 8:31










              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19521146%2fcalculate-age-based-on-date-of-birth%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              10 Answers
              10






              active

              oldest

              votes








              10 Answers
              10






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              169














              PHP >= 5.3.0



              # object oriented
              $from = new DateTime('1970-02-01');
              $to = new DateTime('today');
              echo $from->diff($to)->y;

              # procedural
              echo date_diff(date_create('1970-02-01'), date_create('today'))->y;


              demo



              functions: date_create(), date_diff()




              MySQL >= 5.0.0



              SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', CURDATE()) AS age


              demo



              functions: TIMESTAMPDIFF(), CURDATE()






              share|improve this answer

























              • Fatal error: Call to undefined function date_diff() in

                – PHPupil
                Oct 22 '13 at 14:57











              • @PHPupil: date_diff is for PHP version >= 5.3.0.

                – Glavić
                Oct 22 '13 at 15:07











              • @PHPupil: upgrade PHP? ;) or use MySQL solution.

                – Glavić
                Oct 22 '13 at 15:11











              • This wouldn't work for dob's that are under 1 years old... showing 0 is not necessarily ideal for age

                – Ryman Holmes
                Apr 6 '14 at 15:14






              • 4





                @RymanHolmes: if you need to show 1 year old, instead of 0, show it. One simple if statement solves this. Where is the problem?

                – Glavić
                Apr 6 '14 at 20:01















              169














              PHP >= 5.3.0



              # object oriented
              $from = new DateTime('1970-02-01');
              $to = new DateTime('today');
              echo $from->diff($to)->y;

              # procedural
              echo date_diff(date_create('1970-02-01'), date_create('today'))->y;


              demo



              functions: date_create(), date_diff()




              MySQL >= 5.0.0



              SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', CURDATE()) AS age


              demo



              functions: TIMESTAMPDIFF(), CURDATE()






              share|improve this answer

























              • Fatal error: Call to undefined function date_diff() in

                – PHPupil
                Oct 22 '13 at 14:57











              • @PHPupil: date_diff is for PHP version >= 5.3.0.

                – Glavić
                Oct 22 '13 at 15:07











              • @PHPupil: upgrade PHP? ;) or use MySQL solution.

                – Glavić
                Oct 22 '13 at 15:11











              • This wouldn't work for dob's that are under 1 years old... showing 0 is not necessarily ideal for age

                – Ryman Holmes
                Apr 6 '14 at 15:14






              • 4





                @RymanHolmes: if you need to show 1 year old, instead of 0, show it. One simple if statement solves this. Where is the problem?

                – Glavić
                Apr 6 '14 at 20:01













              169












              169








              169







              PHP >= 5.3.0



              # object oriented
              $from = new DateTime('1970-02-01');
              $to = new DateTime('today');
              echo $from->diff($to)->y;

              # procedural
              echo date_diff(date_create('1970-02-01'), date_create('today'))->y;


              demo



              functions: date_create(), date_diff()




              MySQL >= 5.0.0



              SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', CURDATE()) AS age


              demo



              functions: TIMESTAMPDIFF(), CURDATE()






              share|improve this answer















              PHP >= 5.3.0



              # object oriented
              $from = new DateTime('1970-02-01');
              $to = new DateTime('today');
              echo $from->diff($to)->y;

              # procedural
              echo date_diff(date_create('1970-02-01'), date_create('today'))->y;


              demo



              functions: date_create(), date_diff()




              MySQL >= 5.0.0



              SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', CURDATE()) AS age


              demo



              functions: TIMESTAMPDIFF(), CURDATE()







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Dec 30 '13 at 11:08

























              answered Oct 22 '13 at 14:53









              GlavićGlavić

              33k106086




              33k106086












              • Fatal error: Call to undefined function date_diff() in

                – PHPupil
                Oct 22 '13 at 14:57











              • @PHPupil: date_diff is for PHP version >= 5.3.0.

                – Glavić
                Oct 22 '13 at 15:07











              • @PHPupil: upgrade PHP? ;) or use MySQL solution.

                – Glavić
                Oct 22 '13 at 15:11











              • This wouldn't work for dob's that are under 1 years old... showing 0 is not necessarily ideal for age

                – Ryman Holmes
                Apr 6 '14 at 15:14






              • 4





                @RymanHolmes: if you need to show 1 year old, instead of 0, show it. One simple if statement solves this. Where is the problem?

                – Glavić
                Apr 6 '14 at 20:01

















              • Fatal error: Call to undefined function date_diff() in

                – PHPupil
                Oct 22 '13 at 14:57











              • @PHPupil: date_diff is for PHP version >= 5.3.0.

                – Glavić
                Oct 22 '13 at 15:07











              • @PHPupil: upgrade PHP? ;) or use MySQL solution.

                – Glavić
                Oct 22 '13 at 15:11











              • This wouldn't work for dob's that are under 1 years old... showing 0 is not necessarily ideal for age

                – Ryman Holmes
                Apr 6 '14 at 15:14






              • 4





                @RymanHolmes: if you need to show 1 year old, instead of 0, show it. One simple if statement solves this. Where is the problem?

                – Glavić
                Apr 6 '14 at 20:01
















              Fatal error: Call to undefined function date_diff() in

              – PHPupil
              Oct 22 '13 at 14:57





              Fatal error: Call to undefined function date_diff() in

              – PHPupil
              Oct 22 '13 at 14:57













              @PHPupil: date_diff is for PHP version >= 5.3.0.

              – Glavić
              Oct 22 '13 at 15:07





              @PHPupil: date_diff is for PHP version >= 5.3.0.

              – Glavić
              Oct 22 '13 at 15:07













              @PHPupil: upgrade PHP? ;) or use MySQL solution.

              – Glavić
              Oct 22 '13 at 15:11





              @PHPupil: upgrade PHP? ;) or use MySQL solution.

              – Glavić
              Oct 22 '13 at 15:11













              This wouldn't work for dob's that are under 1 years old... showing 0 is not necessarily ideal for age

              – Ryman Holmes
              Apr 6 '14 at 15:14





              This wouldn't work for dob's that are under 1 years old... showing 0 is not necessarily ideal for age

              – Ryman Holmes
              Apr 6 '14 at 15:14




              4




              4





              @RymanHolmes: if you need to show 1 year old, instead of 0, show it. One simple if statement solves this. Where is the problem?

              – Glavić
              Apr 6 '14 at 20:01





              @RymanHolmes: if you need to show 1 year old, instead of 0, show it. One simple if statement solves this. Where is the problem?

              – Glavić
              Apr 6 '14 at 20:01













              10














              Very small code to get Age:



              <?php
              $dob='1981-10-07';
              $diff = (date('Y') - date('Y',strtotime($dob)));
              echo $diff;
              ?>

              //output 35





              share|improve this answer




















              • 6





                How did this get 11 votes? A person's age depends on the day and month of the year as well.

                – Nick Bedford
                Jun 28 '17 at 4:31











              • Your method is incorrect if person have DOB ex. 13-10-1991 and current month is July then the age is 27 which is wrong. The person age is 26 till the current month is equal to or greater then the DOB month.

                – Shaan Ansari
                Oct 1 '18 at 6:56















              10














              Very small code to get Age:



              <?php
              $dob='1981-10-07';
              $diff = (date('Y') - date('Y',strtotime($dob)));
              echo $diff;
              ?>

              //output 35





              share|improve this answer




















              • 6





                How did this get 11 votes? A person's age depends on the day and month of the year as well.

                – Nick Bedford
                Jun 28 '17 at 4:31











              • Your method is incorrect if person have DOB ex. 13-10-1991 and current month is July then the age is 27 which is wrong. The person age is 26 till the current month is equal to or greater then the DOB month.

                – Shaan Ansari
                Oct 1 '18 at 6:56













              10












              10








              10







              Very small code to get Age:



              <?php
              $dob='1981-10-07';
              $diff = (date('Y') - date('Y',strtotime($dob)));
              echo $diff;
              ?>

              //output 35





              share|improve this answer















              Very small code to get Age:



              <?php
              $dob='1981-10-07';
              $diff = (date('Y') - date('Y',strtotime($dob)));
              echo $diff;
              ?>

              //output 35






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Apr 26 '16 at 1:41









              Mogsdad

              33.4k1290194




              33.4k1290194










              answered Feb 11 '16 at 10:26









              easycodingclubeasycodingclub

              23335




              23335







              • 6





                How did this get 11 votes? A person's age depends on the day and month of the year as well.

                – Nick Bedford
                Jun 28 '17 at 4:31











              • Your method is incorrect if person have DOB ex. 13-10-1991 and current month is July then the age is 27 which is wrong. The person age is 26 till the current month is equal to or greater then the DOB month.

                – Shaan Ansari
                Oct 1 '18 at 6:56












              • 6





                How did this get 11 votes? A person's age depends on the day and month of the year as well.

                – Nick Bedford
                Jun 28 '17 at 4:31











              • Your method is incorrect if person have DOB ex. 13-10-1991 and current month is July then the age is 27 which is wrong. The person age is 26 till the current month is equal to or greater then the DOB month.

                – Shaan Ansari
                Oct 1 '18 at 6:56







              6




              6





              How did this get 11 votes? A person's age depends on the day and month of the year as well.

              – Nick Bedford
              Jun 28 '17 at 4:31





              How did this get 11 votes? A person's age depends on the day and month of the year as well.

              – Nick Bedford
              Jun 28 '17 at 4:31













              Your method is incorrect if person have DOB ex. 13-10-1991 and current month is July then the age is 27 which is wrong. The person age is 26 till the current month is equal to or greater then the DOB month.

              – Shaan Ansari
              Oct 1 '18 at 6:56





              Your method is incorrect if person have DOB ex. 13-10-1991 and current month is July then the age is 27 which is wrong. The person age is 26 till the current month is equal to or greater then the DOB month.

              – Shaan Ansari
              Oct 1 '18 at 6:56











              4














              Got this script from net (thanks to coffeecupweb)



              <?php
              /**
              * Simple PHP age Calculator
              *
              * Calculate and returns age based on the date provided by the user.
              * @param date of birth('Format:yyyy-mm-dd').
              * @return age based on date of birth
              */
              function ageCalculator($dob)
              if(!empty($dob))
              $birthdate = new DateTime($dob);
              $today = new DateTime('today');
              $age = $birthdate->diff($today)->y;
              return $age;
              else
              return 0;


              $dob = '1992-03-18';
              echo ageCalculator($dob);
              ?>





              share|improve this answer























              • works for me thanks

                – Dennis Heiden
                Feb 16 '16 at 15:15















              4














              Got this script from net (thanks to coffeecupweb)



              <?php
              /**
              * Simple PHP age Calculator
              *
              * Calculate and returns age based on the date provided by the user.
              * @param date of birth('Format:yyyy-mm-dd').
              * @return age based on date of birth
              */
              function ageCalculator($dob)
              if(!empty($dob))
              $birthdate = new DateTime($dob);
              $today = new DateTime('today');
              $age = $birthdate->diff($today)->y;
              return $age;
              else
              return 0;


              $dob = '1992-03-18';
              echo ageCalculator($dob);
              ?>





              share|improve this answer























              • works for me thanks

                – Dennis Heiden
                Feb 16 '16 at 15:15













              4












              4








              4







              Got this script from net (thanks to coffeecupweb)



              <?php
              /**
              * Simple PHP age Calculator
              *
              * Calculate and returns age based on the date provided by the user.
              * @param date of birth('Format:yyyy-mm-dd').
              * @return age based on date of birth
              */
              function ageCalculator($dob)
              if(!empty($dob))
              $birthdate = new DateTime($dob);
              $today = new DateTime('today');
              $age = $birthdate->diff($today)->y;
              return $age;
              else
              return 0;


              $dob = '1992-03-18';
              echo ageCalculator($dob);
              ?>





              share|improve this answer













              Got this script from net (thanks to coffeecupweb)



              <?php
              /**
              * Simple PHP age Calculator
              *
              * Calculate and returns age based on the date provided by the user.
              * @param date of birth('Format:yyyy-mm-dd').
              * @return age based on date of birth
              */
              function ageCalculator($dob)
              if(!empty($dob))
              $birthdate = new DateTime($dob);
              $today = new DateTime('today');
              $age = $birthdate->diff($today)->y;
              return $age;
              else
              return 0;


              $dob = '1992-03-18';
              echo ageCalculator($dob);
              ?>






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Oct 11 '15 at 1:25









              googli.usgoogli.us

              411




              411












              • works for me thanks

                – Dennis Heiden
                Feb 16 '16 at 15:15

















              • works for me thanks

                – Dennis Heiden
                Feb 16 '16 at 15:15
















              works for me thanks

              – Dennis Heiden
              Feb 16 '16 at 15:15





              works for me thanks

              – Dennis Heiden
              Feb 16 '16 at 15:15











              2














              Reference Link http://www.calculator.net/age-calculator.html



              $hours_in_day = 24;
              $minutes_in_hour= 60;
              $seconds_in_mins= 60;

              $birth_date = new DateTime("1988-07-31T00:00:00");
              $current_date = new DateTime();

              $diff = $birth_date->diff($current_date);

              echo $years = $diff->y . " years " . $diff->m . " months " . $diff->d . " day(s)"; echo "<br/>";
              echo $months = ($diff->y * 12) + $diff->m . " months " . $diff->d . " day(s)"; echo "<br/>";
              echo $weeks = floor($diff->days/7) . " weeks " . $diff->d%7 . " day(s)"; echo "<br/>";
              echo $days = $diff->days . " days"; echo "<br/>";
              echo $hours = $diff->h + ($diff->days * $hours_in_day) . " hours"; echo "<br/>";
              echo $mins = $diff->h + ($diff->days * $hours_in_day * $minutes_in_hour) . " minutest"; echo "<br/>";
              echo $seconds = $diff->h + ($diff->days * $hours_in_day * $minutes_in_hour * $seconds_in_mins) . " seconds"; echo "<br/>";





              share|improve this answer



























                2














                Reference Link http://www.calculator.net/age-calculator.html



                $hours_in_day = 24;
                $minutes_in_hour= 60;
                $seconds_in_mins= 60;

                $birth_date = new DateTime("1988-07-31T00:00:00");
                $current_date = new DateTime();

                $diff = $birth_date->diff($current_date);

                echo $years = $diff->y . " years " . $diff->m . " months " . $diff->d . " day(s)"; echo "<br/>";
                echo $months = ($diff->y * 12) + $diff->m . " months " . $diff->d . " day(s)"; echo "<br/>";
                echo $weeks = floor($diff->days/7) . " weeks " . $diff->d%7 . " day(s)"; echo "<br/>";
                echo $days = $diff->days . " days"; echo "<br/>";
                echo $hours = $diff->h + ($diff->days * $hours_in_day) . " hours"; echo "<br/>";
                echo $mins = $diff->h + ($diff->days * $hours_in_day * $minutes_in_hour) . " minutest"; echo "<br/>";
                echo $seconds = $diff->h + ($diff->days * $hours_in_day * $minutes_in_hour * $seconds_in_mins) . " seconds"; echo "<br/>";





                share|improve this answer

























                  2












                  2








                  2







                  Reference Link http://www.calculator.net/age-calculator.html



                  $hours_in_day = 24;
                  $minutes_in_hour= 60;
                  $seconds_in_mins= 60;

                  $birth_date = new DateTime("1988-07-31T00:00:00");
                  $current_date = new DateTime();

                  $diff = $birth_date->diff($current_date);

                  echo $years = $diff->y . " years " . $diff->m . " months " . $diff->d . " day(s)"; echo "<br/>";
                  echo $months = ($diff->y * 12) + $diff->m . " months " . $diff->d . " day(s)"; echo "<br/>";
                  echo $weeks = floor($diff->days/7) . " weeks " . $diff->d%7 . " day(s)"; echo "<br/>";
                  echo $days = $diff->days . " days"; echo "<br/>";
                  echo $hours = $diff->h + ($diff->days * $hours_in_day) . " hours"; echo "<br/>";
                  echo $mins = $diff->h + ($diff->days * $hours_in_day * $minutes_in_hour) . " minutest"; echo "<br/>";
                  echo $seconds = $diff->h + ($diff->days * $hours_in_day * $minutes_in_hour * $seconds_in_mins) . " seconds"; echo "<br/>";





                  share|improve this answer













                  Reference Link http://www.calculator.net/age-calculator.html



                  $hours_in_day = 24;
                  $minutes_in_hour= 60;
                  $seconds_in_mins= 60;

                  $birth_date = new DateTime("1988-07-31T00:00:00");
                  $current_date = new DateTime();

                  $diff = $birth_date->diff($current_date);

                  echo $years = $diff->y . " years " . $diff->m . " months " . $diff->d . " day(s)"; echo "<br/>";
                  echo $months = ($diff->y * 12) + $diff->m . " months " . $diff->d . " day(s)"; echo "<br/>";
                  echo $weeks = floor($diff->days/7) . " weeks " . $diff->d%7 . " day(s)"; echo "<br/>";
                  echo $days = $diff->days . " days"; echo "<br/>";
                  echo $hours = $diff->h + ($diff->days * $hours_in_day) . " hours"; echo "<br/>";
                  echo $mins = $diff->h + ($diff->days * $hours_in_day * $minutes_in_hour) . " minutest"; echo "<br/>";
                  echo $seconds = $diff->h + ($diff->days * $hours_in_day * $minutes_in_hour * $seconds_in_mins) . " seconds"; echo "<br/>";






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 13 '16 at 10:00









                  Shailesh SonareShailesh Sonare

                  995910




                  995910





















                      0














                      For a birthday date with format Date/Month/Year



                      function age($birthday)
                      list($day, $month, $year) = explode("/", $birthday);
                      $year_diff = date("Y") - $year;
                      $month_diff = date("m") - $month;
                      $day_diff = date("d") - $day;
                      if ($day_diff < 0 && $month_diff==0) $year_diff--;
                      if ($day_diff < 0 && $month_diff < 0) $year_diff--;
                      return $year_diff;



                      or the same function that accepts day, month, year as parameters :



                      function age($day, $month, $year)
                      $year_diff = date("Y") - $year;
                      $month_diff = date("m") - $month;
                      $day_diff = date("d") - $day;
                      if ($day_diff < 0 && $month_diff==0) $year_diff--;
                      if ($day_diff < 0 && $month_diff < 0) $year_diff--;
                      return $year_diff;



                      You can use it like this :



                      echo age("20/01/2000");


                      which will output the correct age (On 4 June, it's 14).






                      share|improve this answer





























                        0














                        For a birthday date with format Date/Month/Year



                        function age($birthday)
                        list($day, $month, $year) = explode("/", $birthday);
                        $year_diff = date("Y") - $year;
                        $month_diff = date("m") - $month;
                        $day_diff = date("d") - $day;
                        if ($day_diff < 0 && $month_diff==0) $year_diff--;
                        if ($day_diff < 0 && $month_diff < 0) $year_diff--;
                        return $year_diff;



                        or the same function that accepts day, month, year as parameters :



                        function age($day, $month, $year)
                        $year_diff = date("Y") - $year;
                        $month_diff = date("m") - $month;
                        $day_diff = date("d") - $day;
                        if ($day_diff < 0 && $month_diff==0) $year_diff--;
                        if ($day_diff < 0 && $month_diff < 0) $year_diff--;
                        return $year_diff;



                        You can use it like this :



                        echo age("20/01/2000");


                        which will output the correct age (On 4 June, it's 14).






                        share|improve this answer



























                          0












                          0








                          0







                          For a birthday date with format Date/Month/Year



                          function age($birthday)
                          list($day, $month, $year) = explode("/", $birthday);
                          $year_diff = date("Y") - $year;
                          $month_diff = date("m") - $month;
                          $day_diff = date("d") - $day;
                          if ($day_diff < 0 && $month_diff==0) $year_diff--;
                          if ($day_diff < 0 && $month_diff < 0) $year_diff--;
                          return $year_diff;



                          or the same function that accepts day, month, year as parameters :



                          function age($day, $month, $year)
                          $year_diff = date("Y") - $year;
                          $month_diff = date("m") - $month;
                          $day_diff = date("d") - $day;
                          if ($day_diff < 0 && $month_diff==0) $year_diff--;
                          if ($day_diff < 0 && $month_diff < 0) $year_diff--;
                          return $year_diff;



                          You can use it like this :



                          echo age("20/01/2000");


                          which will output the correct age (On 4 June, it's 14).






                          share|improve this answer















                          For a birthday date with format Date/Month/Year



                          function age($birthday)
                          list($day, $month, $year) = explode("/", $birthday);
                          $year_diff = date("Y") - $year;
                          $month_diff = date("m") - $month;
                          $day_diff = date("d") - $day;
                          if ($day_diff < 0 && $month_diff==0) $year_diff--;
                          if ($day_diff < 0 && $month_diff < 0) $year_diff--;
                          return $year_diff;



                          or the same function that accepts day, month, year as parameters :



                          function age($day, $month, $year)
                          $year_diff = date("Y") - $year;
                          $month_diff = date("m") - $month;
                          $day_diff = date("d") - $day;
                          if ($day_diff < 0 && $month_diff==0) $year_diff--;
                          if ($day_diff < 0 && $month_diff < 0) $year_diff--;
                          return $year_diff;



                          You can use it like this :



                          echo age("20/01/2000");


                          which will output the correct age (On 4 June, it's 14).







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jun 4 '14 at 16:10

























                          answered Oct 22 '13 at 14:50









                          SubinSubin

                          2,25212354




                          2,25212354





















                              0














                               $dob = $this->dateOfBirth; //Datetime 
                              $currentDate = new DateTime();
                              $dateDiff = $dob->diff($currentDate);
                              $years = $dateDiff->y;
                              $months = $dateDiff->m;
                              $days = $dateDiff->d;
                              $age = $years .' Year(s)';

                              if($years === 0)
                              $age = $months .' Month(s)';
                              if($months === 0)
                              $age = $days .' Day(s)';


                              return $age;





                              share|improve this answer



























                                0














                                 $dob = $this->dateOfBirth; //Datetime 
                                $currentDate = new DateTime();
                                $dateDiff = $dob->diff($currentDate);
                                $years = $dateDiff->y;
                                $months = $dateDiff->m;
                                $days = $dateDiff->d;
                                $age = $years .' Year(s)';

                                if($years === 0)
                                $age = $months .' Month(s)';
                                if($months === 0)
                                $age = $days .' Day(s)';


                                return $age;





                                share|improve this answer

























                                  0












                                  0








                                  0







                                   $dob = $this->dateOfBirth; //Datetime 
                                  $currentDate = new DateTime();
                                  $dateDiff = $dob->diff($currentDate);
                                  $years = $dateDiff->y;
                                  $months = $dateDiff->m;
                                  $days = $dateDiff->d;
                                  $age = $years .' Year(s)';

                                  if($years === 0)
                                  $age = $months .' Month(s)';
                                  if($months === 0)
                                  $age = $days .' Day(s)';


                                  return $age;





                                  share|improve this answer













                                   $dob = $this->dateOfBirth; //Datetime 
                                  $currentDate = new DateTime();
                                  $dateDiff = $dob->diff($currentDate);
                                  $years = $dateDiff->y;
                                  $months = $dateDiff->m;
                                  $days = $dateDiff->d;
                                  $age = $years .' Year(s)';

                                  if($years === 0)
                                  $age = $months .' Month(s)';
                                  if($months === 0)
                                  $age = $days .' Day(s)';


                                  return $age;






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Jul 16 '15 at 12:54









                                  RutendoRutendo

                                  11




                                  11





















                                      0














                                      declare @dateOfBirth date
                                      select @dateOfBirth = '2000-01-01'



                                      SELECT datediff(YEAR,@dateOfBirth,getdate()) as Age






                                      share|improve this answer





























                                        0














                                        declare @dateOfBirth date
                                        select @dateOfBirth = '2000-01-01'



                                        SELECT datediff(YEAR,@dateOfBirth,getdate()) as Age






                                        share|improve this answer



























                                          0












                                          0








                                          0







                                          declare @dateOfBirth date
                                          select @dateOfBirth = '2000-01-01'



                                          SELECT datediff(YEAR,@dateOfBirth,getdate()) as Age






                                          share|improve this answer















                                          declare @dateOfBirth date
                                          select @dateOfBirth = '2000-01-01'



                                          SELECT datediff(YEAR,@dateOfBirth,getdate()) as Age







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Oct 29 '17 at 18:34









                                          JH_

                                          399313




                                          399313










                                          answered Jan 24 '14 at 6:11









                                          YATHIYATHI

                                          193




                                          193





















                                              0














                                              To Calculate age from Date of birth.



                                              $dob = '1991-09-30';
                                              (((int) date("m",strtotime($dob)) >= (int) date('m')) && ((int) date("d",strtotime($dob)) >= (int) date('d')))

                                              ?
                                              $age = (date('Y') - date('Y',strtotime($dob)))
                                              :
                                              $age = (date('Y') - date('Y',strtotime($dob)))-1;


                                              OUTPUT: 26






                                              share|improve this answer





























                                                0














                                                To Calculate age from Date of birth.



                                                $dob = '1991-09-30';
                                                (((int) date("m",strtotime($dob)) >= (int) date('m')) && ((int) date("d",strtotime($dob)) >= (int) date('d')))

                                                ?
                                                $age = (date('Y') - date('Y',strtotime($dob)))
                                                :
                                                $age = (date('Y') - date('Y',strtotime($dob)))-1;


                                                OUTPUT: 26






                                                share|improve this answer



























                                                  0












                                                  0








                                                  0







                                                  To Calculate age from Date of birth.



                                                  $dob = '1991-09-30';
                                                  (((int) date("m",strtotime($dob)) >= (int) date('m')) && ((int) date("d",strtotime($dob)) >= (int) date('d')))

                                                  ?
                                                  $age = (date('Y') - date('Y',strtotime($dob)))
                                                  :
                                                  $age = (date('Y') - date('Y',strtotime($dob)))-1;


                                                  OUTPUT: 26






                                                  share|improve this answer















                                                  To Calculate age from Date of birth.



                                                  $dob = '1991-09-30';
                                                  (((int) date("m",strtotime($dob)) >= (int) date('m')) && ((int) date("d",strtotime($dob)) >= (int) date('d')))

                                                  ?
                                                  $age = (date('Y') - date('Y',strtotime($dob)))
                                                  :
                                                  $age = (date('Y') - date('Y',strtotime($dob)))-1;


                                                  OUTPUT: 26







                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Nov 13 '18 at 8:43









                                                  Suraj Rao

                                                  23.2k85770




                                                  23.2k85770










                                                  answered Oct 1 '18 at 7:43









                                                  Shaan AnsariShaan Ansari

                                                  1345




                                                  1345





















                                                      0














                                                      I hope you will find this useful.



                                                      $query1="SELECT TIMESTAMPDIFF (YEAR, YOUR_DOB_COLUMN, CURDATE()) AS age FROM your_table WHERE id='$user_id'";
                                                      $res1=mysql_query($query1);
                                                      $row=mysql_fetch_array($res1);
                                                      echo $row['age'];





                                                      share|improve this answer





























                                                        0














                                                        I hope you will find this useful.



                                                        $query1="SELECT TIMESTAMPDIFF (YEAR, YOUR_DOB_COLUMN, CURDATE()) AS age FROM your_table WHERE id='$user_id'";
                                                        $res1=mysql_query($query1);
                                                        $row=mysql_fetch_array($res1);
                                                        echo $row['age'];





                                                        share|improve this answer



























                                                          0












                                                          0








                                                          0







                                                          I hope you will find this useful.



                                                          $query1="SELECT TIMESTAMPDIFF (YEAR, YOUR_DOB_COLUMN, CURDATE()) AS age FROM your_table WHERE id='$user_id'";
                                                          $res1=mysql_query($query1);
                                                          $row=mysql_fetch_array($res1);
                                                          echo $row['age'];





                                                          share|improve this answer















                                                          I hope you will find this useful.



                                                          $query1="SELECT TIMESTAMPDIFF (YEAR, YOUR_DOB_COLUMN, CURDATE()) AS age FROM your_table WHERE id='$user_id'";
                                                          $res1=mysql_query($query1);
                                                          $row=mysql_fetch_array($res1);
                                                          echo $row['age'];






                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited Nov 13 '18 at 10:10

























                                                          answered Nov 13 '18 at 8:38









                                                          Sushant SamletiSushant Samleti

                                                          12




                                                          12





















                                                              -1














                                                              There is a simple way to find the date from any birthdate by using substr of PHP



                                                              $birth_date = '15.03.2014';
                                                              $date = substr($birth_date, 0, 2);
                                                              echo $date;


                                                              Which will just simply give you the output date of that birth date.



                                                              In this case, that will be 15.



                                                              See substr of PHP for more...






                                                              share|improve this answer























                                                              • The question asks for the age, not the 2-digit year someone was born in.

                                                                – Martijn Pieters
                                                                Oct 1 '18 at 8:31















                                                              -1














                                                              There is a simple way to find the date from any birthdate by using substr of PHP



                                                              $birth_date = '15.03.2014';
                                                              $date = substr($birth_date, 0, 2);
                                                              echo $date;


                                                              Which will just simply give you the output date of that birth date.



                                                              In this case, that will be 15.



                                                              See substr of PHP for more...






                                                              share|improve this answer























                                                              • The question asks for the age, not the 2-digit year someone was born in.

                                                                – Martijn Pieters
                                                                Oct 1 '18 at 8:31













                                                              -1












                                                              -1








                                                              -1







                                                              There is a simple way to find the date from any birthdate by using substr of PHP



                                                              $birth_date = '15.03.2014';
                                                              $date = substr($birth_date, 0, 2);
                                                              echo $date;


                                                              Which will just simply give you the output date of that birth date.



                                                              In this case, that will be 15.



                                                              See substr of PHP for more...






                                                              share|improve this answer













                                                              There is a simple way to find the date from any birthdate by using substr of PHP



                                                              $birth_date = '15.03.2014';
                                                              $date = substr($birth_date, 0, 2);
                                                              echo $date;


                                                              Which will just simply give you the output date of that birth date.



                                                              In this case, that will be 15.



                                                              See substr of PHP for more...







                                                              share|improve this answer












                                                              share|improve this answer



                                                              share|improve this answer










                                                              answered Jun 23 '17 at 6:55









                                                              Maniruzzaman AkashManiruzzaman Akash

                                                              1,5911316




                                                              1,5911316












                                                              • The question asks for the age, not the 2-digit year someone was born in.

                                                                – Martijn Pieters
                                                                Oct 1 '18 at 8:31

















                                                              • The question asks for the age, not the 2-digit year someone was born in.

                                                                – Martijn Pieters
                                                                Oct 1 '18 at 8:31
















                                                              The question asks for the age, not the 2-digit year someone was born in.

                                                              – Martijn Pieters
                                                              Oct 1 '18 at 8:31





                                                              The question asks for the age, not the 2-digit year someone was born in.

                                                              – Martijn Pieters
                                                              Oct 1 '18 at 8:31

















                                                              draft saved

                                                              draft discarded
















































                                                              Thanks for contributing an answer to Stack Overflow!


                                                              • Please be sure to answer the question. Provide details and share your research!

                                                              But avoid


                                                              • Asking for help, clarification, or responding to other answers.

                                                              • Making statements based on opinion; back them up with references or personal experience.

                                                              To learn more, see our tips on writing great answers.




                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function ()
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19521146%2fcalculate-age-based-on-date-of-birth%23new-answer', 'question_page');

                                                              );

                                                              Post as a guest















                                                              Required, but never shown





















































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown

































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown







                                                              Popular posts from this blog

                                                              Use pre created SQLite database for Android project in kotlin

                                                              Darth Vader #20

                                                              Ondo