I’m looking for standards for Date/Time addition. I haven’t been able to find any. In particular I’m hoping to find a spec that defines what should happen when you add a month to a date like January 31st. Is the right answer February 28th(/29th)? March 1st? March 2nd?
I’ve seen inconsistent implementations between different tools (PHP & MySQL in this case), and I’m trying to find some sort of standards to base my work on.
Differing Results:
PHP
$end = strtotime("+1 month", 1314835200);
//1317513600 Sat, 01 Oct 2011 20:00:00 -0400
MySQL
SELECT UNIX_TIMESTAMP(DATE_ADD(FROM_UNIXTIME(1314835200), INTERVAL 1 MONTH));
#1317427200 Fri, 30 Sep 2011 20:00:00 -0400
Oracle
SELECT ADD_MONTHS('31-Aug-11', 1) FROM dual;
#30-SEP-11
(sorry for the format change, my oracle foo is weak)
Java
Calendar c = Calendar.getInstance();
c.clear();
c.set( 2011, Calendar.AUGUST, 31 );
c.add( Calendar.MONTH, 1 );
c.getTime()
#Fri Sep 30 00:00:00 EDT 2011
According to the POSIX.1-2001 standard, next month (as in incrementing
tm_monbefore callingmktime) is done by adjusting the values until they fit. So, for example, next month from January 31, 2001 is March 3, 2001. This is because thetm_mdayof 31 isn’t valid withtm_monof 1 (February), so it is normalized totm_monof 2 (March) andtm_mdayof 3.The next month from January 31, 2000 is March 2, 2000, because Feb. has 29 days that year. The next month from January, 1 2038 doesn’t exist, depending.
The great thing about standards is there are so many to chose from. Check the SQL standard, I bet you can find a different meaning of next month. I suspect ISO 8601 may give you yet another choice. Point is, there are many different behaviors, the meaning of ‘next month’ is very domain-specific.
edit: I think I’ve found how SQL-92 handles it, apparently asking for next month from January 31 is an error.
Links: