Jump to content


Photo

Date Difference Problem


  • Please log in to reply
2 replies to this topic

#1 fohanlon

fohanlon
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 07 March 2006 - 12:47 PM

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.

#2 Toni_montana

Toni_montana
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 07 March 2006 - 01:27 PM

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) View Post[/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]


#3 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 07 March 2006 - 04:03 PM

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.

<?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";

?>

I hope this helps.

Happy coding!

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users