[UPHPU] mysql date calculation bug

Jim Anderson jim.anderson at techiegroup.com
Mon Oct 15 15:12:12 MDT 2007


> Ben Reece wrote:
>> I think this is the proper functionality.  The middle 4 queries are 
>> trying to subtract 6 months from:
>>
>> 2007-08-31
>> 2007-08-30
>> 2007-08-29
>> 2007-08-28
>>
>> Since 6 months prior to Aug is Feb, and the last day of Feb is the 
>> 28th, it makes sense to me that Feb 28 would be six months prior to 
>> Aug 31.  The alternative seems to be some time in early March, but 
>> that would only be 5 months previous, which makes no sense.
>> You get the same result if you try to go to a month with only 30 days 
>> -- DATE_SUB( DATE_SUB(CURDATE(), INTERVAL 45 DAY), INTERVAL 2 MONTH) 
>> will return 2007-06-30, so I don't think it has anything to do with a 
>> February/leap year problem.
>>
>> Ben

> Jennifer Charrey wrote:
> This is expected behavior and you can find it in the MySQL documentation:
>
> >  If you add MONTH, YEAR_MONTH, or YEAR and the resulting date has a 
> day that is larger than the maximum day for the new month, the day is 
> adjusted to the maximum days in the new month:
> >
> > mysql> SELECT DATE_ADD('1998-01-30', INTERVAL 1 MONTH);
> >         -> '1998-02-28'
>
> http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add 
>
>
> As Ben said, a different answer probably wouldn't even make sense. 
> March 3 would not be 6 months ago.

Yeah, further digging on the mysql developers site gave me this reply as 
well. I guess I will need to flesh something out that will handle that 
scenario because you are right, it does kind-of make sense.


thnx,
-jim



More information about the UPHPU mailing list