Hi All,
I am wondering what the best way to determine an age of something/somone is.
For instance, let's say I have two dates:
1985-07-19
2003-11-30
Now, I want to determine the interval in years, and for each fraction of a
year, days. I won't mess with months because they can have variable length,
or if I do measure months I will simply say that a month is 30 days.
So here's my problem: normally when doing date comparison I simply convert
the dates to a UNIX timestamp. This allows me to get the number of seconds
from one date to the other. This is fine for small date ranges. For
instance, let's say I had these dates:
1985-07-19
1985-11-15
Once I get the number of seconds, I simply divide by 86400 in order to
determine the number of days.
But things get complicated once the date range gets large enough, because I
now have to worry about leap years. Every 4 years we have an extra 86400
seconds in a year. So how do I handle this case? If I want to know the
number years and days for two dates that are far apart, like the two I
listed at the beginning, what should I be doing? How do I account for leap
years, where there is an extra day?
I'm looking at PHP's builtin date and time functions, and nothing seems to
handle the case I've got in mind. Do I have to roll my own solution here
and check to see whether or not each year in my daterange is a leap year?
Thanks for any pointers!
Sincerely,
-Josh
I am wondering what the best way to determine an age of something/somone is.
For instance, let's say I have two dates:
1985-07-19
2003-11-30
Now, I want to determine the interval in years, and for each fraction of a
year, days. I won't mess with months because they can have variable length,
or if I do measure months I will simply say that a month is 30 days.
So here's my problem: normally when doing date comparison I simply convert
the dates to a UNIX timestamp. This allows me to get the number of seconds
from one date to the other. This is fine for small date ranges. For
instance, let's say I had these dates:
1985-07-19
1985-11-15
Once I get the number of seconds, I simply divide by 86400 in order to
determine the number of days.
But things get complicated once the date range gets large enough, because I
now have to worry about leap years. Every 4 years we have an extra 86400
seconds in a year. So how do I handle this case? If I want to know the
number years and days for two dates that are far apart, like the two I
listed at the beginning, what should I be doing? How do I account for leap
years, where there is an extra day?
I'm looking at PHP's builtin date and time functions, and nothing seems to
handle the case I've got in mind. Do I have to roll my own solution here
and check to see whether or not each year in my daterange is a leap year?
Thanks for any pointers!
Sincerely,
-Josh
Comment