470,815 Members | 1,292 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

Diff Between 2 Dates

I am trying to find the difference between 2 dates. The problem that I am having is that all
routines in the PHP Manual and all of the scripts I have seen on the Internet only allow a date as
old as 1970. I am trying to find the age of a person in months so 1970 is not far back enough. Does
anyone know of a script where 1970 is not a restriction?

Thanks in advance.

Mike
Mar 4 '06 #1
5 1788
Mike wrote:
I am trying to find the difference between 2 dates. The problem that I am having is that all
routines in the PHP Manual and all of the scripts I have seen on the Internet only allow a date as
old as 1970. I am trying to find the age of a person in months so 1970 is not far back enough. Does
anyone know of a script where 1970 is not a restriction?

Thanks in advance.

Mike

Does the server run Linux or Windows?

--
Carl Vondrick
www.carlsoft.net
usenet [at] carlsoft [dot] net
Mar 4 '06 #2
Mike wrote:
I am trying to find the difference between 2 dates. The problem that
I am having is that all routines in the PHP Manual and all of the
scripts I have seen on the Internet only allow a date as old as 1970.
I am trying to find the age of a person in months so 1970 is not far
back enough. Does anyone know of a script where 1970 is not a
restriction?


Have a look at the calendar extension:

http://www.php.net/calendar
JW
Mar 4 '06 #3

Mike wrote:
I am trying to find the difference between 2 dates. The problem that I am having is that all
routines in the PHP Manual and all of the scripts I have seen on the Internet only allow a date as
old as 1970. I am trying to find the age of a person in months so 1970 is not far back enough. Does
anyone know of a script where 1970 is not a restriction?

Thanks in advance.

Mike


The solution is quite simple. This is how people do it in their heads.
First, substract the year the person is born from the current year.
Then, check whether the person has already celebrated his/her birthday
this year. If not--substract one.

Here's an implementation:

function age($year, $month, $day) {
$now_ts = time();
$now_a = getdate($now_ts);
$age = $now_a['year'] - $year;
$bday_ts = mktime(0, 0, 0, $month, $day, $now_a['year']);
if($now_ts < $bday_ts) {
$age--;
}
return $age;
}

Mar 4 '06 #4
Be aware this works fine for someone's age, but for more precise date
calculations, especially at much older/earlier dates, you will have to
account for all the funny time changes, corrections, and drift that
have taken place (like how they skipped a couple days completely to
make up for lost time). So i would suggest either reading up on
date/time calculations extensively or finding a good preexisting library

Mar 4 '06 #5
>I am trying to find the difference between 2 dates. The problem that I
am having is that all
routines in the PHP Manual and all of the scripts I have seen on the
Internet only allow a date as
old as 1970. I am trying to find the age of a person in months so 1970
is not far back enough. Does
anyone know of a script where 1970 is not a restriction?


PHP suffers from the limitations of a UNIX time_t. I recommend
using MySQL date functions where the allowed range includes the
years 1 thru 9999, which is good enough for most practical uses.

Gordon L. Burditt
Mar 4 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by TonyHa | last post: by
9 posts views Thread by Ching-Lung | last post: by
4 posts views Thread by PawelR | last post: by
3 posts views Thread by Raghu Raman | last post: by
2 posts views Thread by SenthilVel | last post: by
4 posts views Thread by Andreas Kasparek | last post: by
2 posts views Thread by dkrysmann | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.