Calculate age based on date of birth
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
add a comment |
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
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
add a comment |
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
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
php mysql date
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
add a comment |
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
add a comment |
10 Answers
10
active
oldest
votes
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()
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
|
show 1 more comment
Very small code to get Age:
<?php
$dob='1981-10-07';
$diff = (date('Y') - date('Y',strtotime($dob)));
echo $diff;
?>
//output 35
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
add a comment |
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);
?>
works for me thanks
– Dennis Heiden
Feb 16 '16 at 15:15
add a comment |
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/>";
add a comment |
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).
add a comment |
$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;
add a comment |
declare @dateOfBirth date
select @dateOfBirth = '2000-01-01'
SELECT datediff(YEAR,@dateOfBirth,getdate()) as Age
add a comment |
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
add a comment |
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'];
add a comment |
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...
The question asks for the age, not the 2-digit year someone was born in.
– Martijn Pieters♦
Oct 1 '18 at 8:31
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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()
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
|
show 1 more comment
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()
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
|
show 1 more comment
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()
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()
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
|
show 1 more comment
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
|
show 1 more comment
Very small code to get Age:
<?php
$dob='1981-10-07';
$diff = (date('Y') - date('Y',strtotime($dob)));
echo $diff;
?>
//output 35
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
add a comment |
Very small code to get Age:
<?php
$dob='1981-10-07';
$diff = (date('Y') - date('Y',strtotime($dob)));
echo $diff;
?>
//output 35
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
add a comment |
Very small code to get Age:
<?php
$dob='1981-10-07';
$diff = (date('Y') - date('Y',strtotime($dob)));
echo $diff;
?>
//output 35
Very small code to get Age:
<?php
$dob='1981-10-07';
$diff = (date('Y') - date('Y',strtotime($dob)));
echo $diff;
?>
//output 35
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
add a comment |
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
add a comment |
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);
?>
works for me thanks
– Dennis Heiden
Feb 16 '16 at 15:15
add a comment |
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);
?>
works for me thanks
– Dennis Heiden
Feb 16 '16 at 15:15
add a comment |
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);
?>
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);
?>
answered Oct 11 '15 at 1:25
googli.usgoogli.us
411
411
works for me thanks
– Dennis Heiden
Feb 16 '16 at 15:15
add a comment |
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
add a comment |
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/>";
add a comment |
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/>";
add a comment |
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/>";
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/>";
answered Apr 13 '16 at 10:00
Shailesh SonareShailesh Sonare
995910
995910
add a comment |
add a comment |
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).
add a comment |
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).
add a comment |
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).
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).
edited Jun 4 '14 at 16:10
answered Oct 22 '13 at 14:50
SubinSubin
2,25212354
2,25212354
add a comment |
add a comment |
$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;
add a comment |
$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;
add a comment |
$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;
$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;
answered Jul 16 '15 at 12:54
RutendoRutendo
11
11
add a comment |
add a comment |
declare @dateOfBirth date
select @dateOfBirth = '2000-01-01'
SELECT datediff(YEAR,@dateOfBirth,getdate()) as Age
add a comment |
declare @dateOfBirth date
select @dateOfBirth = '2000-01-01'
SELECT datediff(YEAR,@dateOfBirth,getdate()) as Age
add a comment |
declare @dateOfBirth date
select @dateOfBirth = '2000-01-01'
SELECT datediff(YEAR,@dateOfBirth,getdate()) as Age
declare @dateOfBirth date
select @dateOfBirth = '2000-01-01'
SELECT datediff(YEAR,@dateOfBirth,getdate()) as Age
edited Oct 29 '17 at 18:34
JH_
399313
399313
answered Jan 24 '14 at 6:11
YATHIYATHI
193
193
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
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
add a comment |
add a comment |
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'];
add a comment |
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'];
add a comment |
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'];
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'];
edited Nov 13 '18 at 10:10
answered Nov 13 '18 at 8:38
Sushant SamletiSushant Samleti
12
12
add a comment |
add a comment |
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...
The question asks for the age, not the 2-digit year someone was born in.
– Martijn Pieters♦
Oct 1 '18 at 8:31
add a comment |
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...
The question asks for the age, not the 2-digit year someone was born in.
– Martijn Pieters♦
Oct 1 '18 at 8:31
add a comment |
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...
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...
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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