473,387 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problems with using date functions

Hi,

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.

Jul 24 '05 #1
1 2985
>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 ?
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


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.

Jul 24 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Markku Uttula | last post by:
I think I'm doing something wrong. I'm able to connect to Oracle just fine, execute queries and all, but I'm having serious problems with the speed :( For example, the following PHP-script on my...
19
by: Gerson Kurz | last post by:
AAAAAAAARG I hate the way python handles unicode. Here is a nice problem for y'all to enjoy: say you have a variable thats unicode directory = u"c:\temp" Its unicode not because you want it...
0
by: Gernot Saborowski | last post by:
To all IIS/ASP-professionals, we just upgraded from one machine to another and are experiencing some strange date time number problems. Both systems have been set up like this: - MS Windows...
5
by: Jonathan Crawford | last post by:
Hi I am niew to pho and am having trouble with dates. I can format a date ok so $today=date('d M Y' ) gives me 2 Nov 2005 $dateval is a mysql date
2
by: Jonathan \(Pickles\) Sklan-Willis | last post by:
Hi All, This is what I want to do: 1. Build a database in Access with certain queries and a query form. (DONE). 2. I now need to use the same form as a form in VB, this will be the front page...
2
by: ebby83 | last post by:
Hi I am trying to run this basic Timestamp service using RPC . Here are the file for the same . I run it the foll way: gdoshi@hobbes:!:rpcgen date.x gdoshi@hobbes:!:lsdate.h ...
1
by: electrixnow | last post by:
Help!, I need to compile this code with static libs so it run on another XP machine that does'nt have MS Studio installed. When I compile now I get an ERROR: 1>------ Rebuild All started:...
2
by: Jesus | last post by:
Hi! I'm migrating a web application (ASP.NET) from Framework 1.1 to Framework 2.0. I have a problem with that, and I didn't found a solution changing something in web.config or something like...
10
by: jodleren | last post by:
Hi I know, that there are a lot of people having problems with orkut.com, errors like "object expected" and named objects missing. When loading the site can generate some 10 errors, and still...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.