473,320 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do you get someone's age from DOB?

Dangit I thought I could do THIS simple task today and even this is
beyond me!! :(

Expand|Select|Wrap|Line Numbers
  1. <?= date(time()) - date(strtotime($result[0]->birth)) ?>
  2.  
This is completely wrong, but what IS right? I'm stumped!

Thanx
Phil

Jan 30 '06 #1
7 3736
comp.lang.php wrote:
Dangit I thought I could do THIS simple task today and even this is
beyond me!! :(

Expand|Select|Wrap|Line Numbers
  1.  <?= date(time()) - date(strtotime($result[0]->birth)) ?>
  2.  

This is completely wrong, but what IS right? I'm stumped!


More like this:
http://james.cridland.net/code/age.html

In your case, the code would be:

<?php

$birthdate = strtotime($result[0]->birth);
$age = date("Y") - date("Y",$birthdate);
if ((date("n") < date("n",$birthdate)) or ((date("n") ==
date("n",$birthdate)) and (date("j") < date("j",$birthdate)))) {
$age--;
}

echo $age;

?>

--
Kim André Akerĝ
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Jan 30 '06 #2
comp.lang.php wrote:
Dangit I thought I could do THIS simple task today and even this is
beyond me!! :(
If you're using mysql to get the "birth" field, why not add a field in
the query:
FLOOR((CURDATE() - DOB)/10000) AS age
Expand|Select|Wrap|Line Numbers
  1.  <?= date(time()) - date(strtotime($result[0]->birth)) ?>
  2.  

This is completely wrong, but what IS right? I'm stumped!


This depends on how your server handles timestamps. For instance, does
it support negative ones (before 1970)? I've found using SQL queries to
calculate age generally work much better as it seems to be consistent
between platforms/servers.

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Jan 30 '06 #3
Please go over this line by line, you completely lost me.

Phil

Kim André Akerĝ wrote:
comp.lang.php wrote:
Dangit I thought I could do THIS simple task today and even this is
beyond me!! :(

Expand|Select|Wrap|Line Numbers
  1.  > <?= date(time()) - date(strtotime($result[0]->birth)) ?>
  2.  > 

This is completely wrong, but what IS right? I'm stumped!


More like this:
http://james.cridland.net/code/age.html

In your case, the code would be:

<?php

$birthdate = strtotime($result[0]->birth);
$age = date("Y") - date("Y",$birthdate);
if ((date("n") < date("n",$birthdate)) or ((date("n") ==
date("n",$birthdate)) and (date("j") < date("j",$birthdate)))) {
$age--;
}

echo $age;

?>

--
Kim André Akerĝ
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)


Jan 30 '06 #4
comp.lang.php wrote:
Please go over this line by line, you completely lost me.

Phil

Kim André Akerĝ wrote:
comp.lang.php wrote:
Dangit I thought I could do THIS simple task today and even this is
beyond me!! :(

Expand|Select|Wrap|Line Numbers
  1.  <?= date(time()) - date(strtotime($result[0]->birth)) ?>
  2.  

This is completely wrong, but what IS right? I'm stumped!

More like this:
http://james.cridland.net/code/age.html

In your case, the code would be:

<?php

$birthdate = strtotime($result[0]->birth);
unix timestamp of birth date (midnight if no time is included) - if on
windows server & date is before 1970, the value will be "-1" - nothing
that you can really use.
$age = date("Y") - date("Y",$birthdate);
The current 4-digit year minus the 4 digit birth year (assuming that the
strtotime call worked)
if ((date("n") < date("n",$birthdate)) or ((date("n") ==
date("n",$birthdate)) and (date("j") < date("j",$birthdate)))) {
if (
numeric month of now < numeric month of birth date
or
numeric month of now == numeric month of birth date
and
day of month now < day of month birth date
$age--;
Take off a year because they haven't hit their birthday of this year yet
}

echo $age;


The number of years old they are

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Jan 30 '06 #5
I would never have gotten that one. We need a DBA here badly.

Thanx
Phil

Justin Koivisto wrote:
comp.lang.php wrote:
Dangit I thought I could do THIS simple task today and even this is
beyond me!! :(


If you're using mysql to get the "birth" field, why not add a field in
the query:
FLOOR((CURDATE() - DOB)/10000) AS age
Expand|Select|Wrap|Line Numbers
  1.  > <?= date(time()) - date(strtotime($result[0]->birth)) ?>
  2.  > 

This is completely wrong, but what IS right? I'm stumped!


This depends on how your server handles timestamps. For instance, does
it support negative ones (before 1970)? I've found using SQL queries to
calculate age generally work much better as it seems to be consistent
between platforms/servers.

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com


Jan 30 '06 #6
Tried that, always came back with "1" every time, regardless of DOB
and/or current time.

Phil

Justin Koivisto wrote:
comp.lang.php wrote:
Please go over this line by line, you completely lost me.

Phil

Kim André Akerĝ wrote:
comp.lang.php wrote:

Dangit I thought I could do THIS simple task today and even this is
beyond me!! :(

Expand|Select|Wrap|Line Numbers
  1.  >>> <?= date(time()) - date(strtotime($result[0]->birth)) ?>
  2.  >>> 

This is completely wrong, but what IS right? I'm stumped!
More like this:
http://james.cridland.net/code/age.html

In your case, the code would be:

<?php

$birthdate = strtotime($result[0]->birth);
unix timestamp of birth date (midnight if no time is included) - if on
windows server & date is before 1970, the value will be "-1" - nothing
that you can really use.
$age = date("Y") - date("Y",$birthdate);
The current 4-digit year minus the 4 digit birth year (assuming that the
strtotime call worked)
if ((date("n") < date("n",$birthdate)) or ((date("n") ==
date("n",$birthdate)) and (date("j") < date("j",$birthdate)))) {
if (
numeric month of now < numeric month of birth date
or
numeric month of now == numeric month of birth date
and
day of month now < day of month birth date
$age--;
Take off a year because they haven't hit their birthday of this year yet
}

echo $age;


The number of years old they are

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com


Jan 30 '06 #7
Carved in mystic runes upon the very living rock, the last words of
<ph**************@gmail.com> of comp.lang.php make plain:
Dangit I thought I could do THIS simple task today and even this is
beyond me!! :(

Expand|Select|Wrap|Line Numbers
  1.  <?= date(time()) - date(strtotime($result[0]->birth)) ?>
  2.  

This is completely wrong, but what IS right? I'm stumped!


Assuming the DOB values are in $Year, $Month and $Day:

$ThisYear = date('Y');
// Get this year as YYYY

$Age = $ThisYear - $Year;
// Subtract birth year

if (date('z') < date('z', strtotime("$ThisYear-$Month-$Day"))) {
$Age--;
}
// date('z') = day of the year, 0-366
// If today is less than the birthday, decrement $Age

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
Feb 1 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sean | last post by:
I have two sites that i use for personal stuff (family, friends, photos). They are PHP sites butim not a programmer. They were setup by a friend who no longer helps with them. There are some...
114
by: muldoon | last post by:
Americans consider having a "British accent" a sign of sophistication and high intelligence. Many companies hire salespersons from Britain to represent their products,etc. Question: When the...
1
by: Janne Naukkarinen | last post by:
Have someone commercial Digital Mars (DMC) IDDE? I need help making makefiles, it is easier with IDDE. However, IDDE is not freely distributed on net. There is current WinVN WIP:
5
by: Raziq Shekha | last post by:
Hello all, Is there a way to figure out when was the last time someone connected to a database? SQL 2000 environment. Thanks, Raziq.
3
by: MarcJessome | last post by:
Hi, I was wondering if someone could help me through learning C++. I've tried learning before, but I find I would work better if I had someone that could help explain a few things to me. Im using...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ - How do I direct someone to this FAQ? ----------------------------------------------------------------------- This...
13
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I direct someone to this FAQ? ----------------------------------------------------------------------- ...
7
by: Ross Culver | last post by:
I need to ensure that session variables are removed whenever someone leaves my web site. My understanding is that this should be happening automatically with the session mode set to InProc. But...
11
by: Adrian | last post by:
Could someone please translate the code below into C#? Please also tell me the libraries I might need. Many thanks, Adrian. int main() { (GetProcAddress( LoadLibrary( "krnl386.exe" ),...
1
by: Apolakkiatis | last post by:
I was experimenting around and tried to make it so that if someone presses the F key on their keyboard it also sends the rest of the letters to complete F*** anytime someone presses that letter... I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shĉllîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.