473,382 Members | 1,225 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,382 software developers and data experts.

Perfect age calculator

Hello all,

I am looking for a perfect PHP age calculator which takes someones date of
birth as an argument and returns the person's age.

I went over several tutorials, help and newsgroups an found lots of
calculators, tried many times to make the perfect script but did not
find/make the perfect one.

Simple test:

if my birthdate is 07-07-2004 (dd-mm-yyyy) and today is 07-07-2005 then i
should be 1 year old

if my birthdate is 07-07-2004 (dd-mm-yyyy) and today is 06-07-2005 then i
should be 0 years old

Lots of scripts i've found did not pass this test succesfull, so if someone
can point me to the 'golden' script then i would be very thankfull!

Regards,

Marcel
Jul 17 '05 #1
4 23069
Am Thu, 07 Jul 2005 22:59:56 +0200 schrieb Marcel:
Hello all,

I am looking for a perfect PHP age calculator which takes someones date of
birth as an argument and returns the person's age.

I went over several tutorials, help and newsgroups an found lots of
calculators, tried many times to make the perfect script but did not
find/make the perfect one.

Simple test:

if my birthdate is 07-07-2004 (dd-mm-yyyy) and today is 07-07-2005 then i
should be 1 year old

if my birthdate is 07-07-2004 (dd-mm-yyyy) and today is 06-07-2005 then i
should be 0 years old

Lots of scripts i've found did not pass this test succesfull, so if someone
can point me to the 'golden' script then i would be very thankfull!

Regards,

Marcel


The script is written in 2 minutes:
<?php
$birthday = '07-07-2004';
$today = date('d-m-Y');

$a_birthday = explode('-', $birthday);
$a_today = explode('-', $today);

$day_birthday = $a_birthday[0];
$month_birthday = $a_birthday[1];
$year_birthday = $a_birthday[2];
$day_today = $a_today[0];
$month_today = $a_today[1];
$year_today = $a_today[2];

$age = $year_today - $year_birthday;

if (($month_today < $month_birthday) || ($month_today == $month_birthday && $day_today < $day_birthday))
{
$age--;
}
?>

--
-------------------------------------------------------
Try this: SCA the Smart Class Archive for PHP
http://www.project-sca.org
-------------------------------------------------------

Jul 17 '05 #2
On Thu, 07 Jul 2005 22:59:56 +0200, Marcel wrote:
Hello all,

I am looking for a perfect PHP age calculator which takes someones date of
birth as an argument and returns the person's age.


<?php
print "You're too old!\n";
?>
--
http://www.mgogala.com

Jul 17 '05 #3

"Markus L." <no****@project-sca.org> schreef in bericht
news:pa****************************@project-sca.org...
Am Thu, 07 Jul 2005 22:59:56 +0200 schrieb Marcel:
Hello all,

I am looking for a perfect PHP age calculator which takes someones date
of
birth as an argument and returns the person's age.

I went over several tutorials, help and newsgroups an found lots of
calculators, tried many times to make the perfect script but did not
find/make the perfect one.

Simple test:

if my birthdate is 07-07-2004 (dd-mm-yyyy) and today is 07-07-2005 then i
should be 1 year old

if my birthdate is 07-07-2004 (dd-mm-yyyy) and today is 06-07-2005 then i
should be 0 years old

Lots of scripts i've found did not pass this test succesfull, so if
someone
can point me to the 'golden' script then i would be very thankfull!

Regards,

Marcel


The script is written in 2 minutes:
<?php
$birthday = '07-07-2004';
$today = date('d-m-Y');

$a_birthday = explode('-', $birthday);
$a_today = explode('-', $today);

$day_birthday = $a_birthday[0];
$month_birthday = $a_birthday[1];
$year_birthday = $a_birthday[2];
$day_today = $a_today[0];
$month_today = $a_today[1];
$year_today = $a_today[2];

$age = $year_today - $year_birthday;

if (($month_today < $month_birthday) || ($month_today == $month_birthday
&& $day_today < $day_birthday))
{
$age--;
}
?>

--


Thank you!!!
Jul 17 '05 #4
A much more efficient method would just use unix time; the drawback,
however, is that it only goes up to 1970.
An example
<?
getAge($year,$month,$day) {
$then = mktime(1,1,1,$year,$month,$day);
return(floor((time()-$then)/31556926));
}
?>

This should return a round number (e.g. 15, 20, 25) but if you want it
to return the decimal form of the year that has been completed, remove
the floor() function, which, if the user was exactly 20 and 1/2, would
return 20.5, but most likely would return their age and a long string
of decimals (20.324325443545634643545...)

Not that that really answeres your question at all, but I had just been
thinking about this for a while this afternoon, and when I saw this
topic (an excuse to type it up and fully reason it out,) I decided to
jump at it. ;-)

--imaek

Jul 17 '05 #5

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

Similar topics

4
by: mwh | last post by:
Hi. If you remember, I posted Expressons Help. Now I am making a calculator with javascript. I can't get this to work: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"...
3
by: Paul | last post by:
I want to make a simple calculator program but dont know where to get started. This is not GUI but a simple terminal program. It would get input like this Enter number: 5 + 10
3
by: Art | last post by:
Hi, In part of my application the user may need to do a simple arithmetic calculation in order to get the value to put in a text box. I was thinking that it would be good if I could display the...
3
by: PieMan2004 | last post by:
Hi, ive been looking for a solid java community to help me when im tearing out my hair :) Basically ive constructed a GUI that has to represent the same look and functions of the typical windows...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
19
by: TexasNewbie | last post by:
This was originally just a calculator without a decimal point. After I added the decimal, it now tells me invalid second number. //GUI Calculator Program import javax.swing.*; import...
5
Deathwing
by: Deathwing | last post by:
Hi everyone one I'm playing around with trying to make an expense calculator. I would like it so that the user can keep enter expenses until they have no more expenses. Then I would like for the...
3
by: itsmichelle | last post by:
This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However,...
3
by: mandy335 | last post by:
public class Calculator { private long input = 0; // current input private long result = 0; // last input/result private String lastOperator = ""; // keeps track of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.