Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Subject
- Posted on
- Problems with using date functions
- 07-24-2005
- sylvian stone
July 24, 2005, 8:13 pm
I've used standard date functions in the past, but need to create
something a little different, as I am working on an investment
calculator.
What I need to do is validate two dates, and check for a complete
years. And a complete year is from day 1 until the proceeding day the
following year. A couple of examples:
01/01/2000 ---> 31/12/2000 --> 1 Complete year (Year 1)
01/01/2001 ---> 31/12/2001 --> 1 Complete year (Year 2)
01/01/2002 ---> 01/07/2002 --> 1 incomplete year (Year 3)
another example
31/01/2000 ---> 30/01/2001 ---> 1 Complete Year (Year 1)
I can probably manage to calculate the number of complete and
incomplete years, but how do I work out '1 Complete Year' from PHP's
internal functions ?
P.S. This may not be logical, but it is for a calculation devised by
the UK Inland Revenue .....
Thanks in advance for any suggestions...
Rgds
SS.
Re: Problems with using date functions
Ok, first the algorithm:
1. take the end of the range, add one day, and call it the "end date"
(ok, this is being a bit loose with terminology: I don't care. It's
really the day after the end). break this up into the "end year",
"end month", and "end day".
2. take the start of the range, and call this the "start date".
break this up into the "start year", "start month", and "start day".
3. Subtract the start year from the end year. This is the tentative
number of complete years.
4. If the end month is less than the start month, the number of complete
years is the number from (3) minus one.
5. If the end month is equal to the start month, and the end day is less
than the start day, the number of complete years is the number from
(3) minus one.
6. If (4) and (5) don't apply, the number from (3) is the number of complete
years.
I am assuming here that:
29/2/1996 -> 28/2/1997 *IS* one complete year, and
28/2/1996 -> 28/2/1997 *IS* one complete year, and
1/3/1995 -> 29/2/1996 *IS* one complete year, and
1/3/1995 -> 28/2/1996 *IS NOT* one complete year.
This seems reasonable, but you never can tell with tax laws.
In MySQL, assuming you start with a DATE type, you can use ADDDATE()
to add one day, and break apart the pieces of the date with YEAR(),
MONTH(), and DAYOFMONTH() for (1) and (2). (WARNING: MySQL date
types have a Y10K problem, but this is unlikely to be a real problem
for tax and investment calculations).
The rest is math on single numbers, which can be done with IF() and
subtraction and comparison operators.
In PHP, convert the date into a UNIX timestamp (PROBLEM HERE: if
dates can legitimately extend before 1970, expect trouble, as holding
an investment for 36 years or more is certainly not unheard of)
with strtotime(), add 24*60*60 seconds, and then break it apart
again with localtime() for (1) and (2). An alternative is to break
the dates apart manually and add 1 day to the end manually, using
a lot of knowledge about the lengths of months and leap year rules
to propagate carries from the day to the month to the year as needed.
The rest is math on single numbers, which can be done with if and
subtraction and comparison operators.
Gordon L. Burditt
Site Timeline
- » PHP Guestbook list on PHPKode.com
- — Next thread in » PHP Scripting Forum
- » PHPDocumentor
- — Previous thread in » PHP Scripting Forum
- » URL redirection
- — Newest thread in » PHP Scripting Forum
- » ssh on command line: force using a group size (prime size) of 1024 (and no...
- — The site's Newest Thread. Posted in » Secure Shell Forum