Jump to content

Archived

This topic is now archived and is closed to further replies.

fohanlon

Date Difference Problem

Recommended Posts

Hi

I need to be able to calculate the exact age of a person based on a comparison to todays date.

I have tried to use timestamp but I run into the 1970 problem. The dob the user can choose can go from 1st jan 1910.

For example:

Today is the 7th march 2006

Using the following dates I need to be able to get the results below:

DOB: 6th March 1988 - return whether 18 or not

DOB: 7th March 1988 - return whether 18 or not

DOB: 8th March 1988 - return whether 18 or not

I have searched the web but all code rounds to nearest year but I need to be exact as the solution will determine a calculated cost.

Any help would be greatly appreciated.

Thanks,

Fergal.

Share this post


Link to post
Share on other sites
In my opinion, you don't need the exact age to tell wheter someon is above 18 or not.

Just use one of the solutions you found, and instead of round() use floor().

This will round the age down.

good luck

rogier


[!--quoteo(post=352475:date=Mar 7 2006, 07:47 AM:name=fohanlon)--][div class=\'quotetop\']QUOTE(fohanlon @ Mar 7 2006, 07:47 AM) [snapback]352475[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hi

I need to be able to calculate the exact age of a person based on a comparison to todays date.

I have tried to use timestamp but I run into the 1970 problem. The dob the user can choose can go from 1st jan 1910.

For example:

Today is the 7th march 2006

Using the following dates I need to be able to get the results below:

DOB: 6th March 1988 - return whether 18 or not

DOB: 7th March 1988 - return whether 18 or not

DOB: 8th March 1988 - return whether 18 or not

I have searched the web but all code rounds to nearest year but I need to be exact as the solution will determine a calculated cost.

Any help would be greatly appreciated.

Thanks,

Fergal.
[/quote]

Share this post


Link to post
Share on other sites
If all you want to know is the age in years then exact age is not important, but I think I have a script that will fit your need.

Assuming the following code is in age.php
date should be passed as age.php?date=6/5/1988
You can change the way it gets the date to suit your needs, but the basic idea is here.

This is just what I came up with off the top of my head. There are probably a handful of ways to do it.

[code]
<?php

$arr1 = explode("/",$_GET['date']);
$arr2 = array(date("m"),date("d"),date("Y"));

$year = $arr2[2] - $arr1[2];

if ( ( $arr1[0] > $arr2[0] ) || ( ( $arr1[0] = $arr2[0] ) && ( $arr1[1] < $arr2[1] ) ) ) $year = $year - 1;

echo "<p>User born on ".$_GET['date']." is ".$year." years old.</p>\r\n";
if ( $year < 21 ) echo "<p>User is under 21.</p>\r\n";
if ( $year < 18 ) echo "<p>User is a minor.</p>\r\n";

?>
[/code]

I hope this helps.

Happy coding!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.