talideon.com

...randomly generated messages

March 4, 2008 at 3:55PM Handling of leap days in PHP and MySQL

Niall was wondering earlier about how our billing system here at work handles leap days. I wasn’t entirely sure because I wasn’t sure how exactly PHP and MySQL handle them and if they handle them the same way.

It turns out PHP and MySQL handle leap days completely differently. PHP will chose March 1st of the next year when incrementing a leap-day date, while MySQL will chose February 28th. Both can be valid ways of handling the special case, but they’re plenty of cases where you’d prefer one of them over the other. For instance, with domain renewals, it’s better to renew early rather than late because a domain might go into redemption or quarantine otherwise.

Here’s my checks. First, PHP:

<?php
echo date('c', strtotime('+1 year', strtotime('February 29th, 2008')));
2009-03-01T00:00:00+00:00

And here’s MySQL:

mysql> select date_add('2008-02-29 00:00:00', interval 1 year);
+--------------------------------------------------+
| date_add('2008-02-29 00:00:00', interval 1 year) |
+--------------------------------------------------+
| 2009-02-28 00:00:00                              |
+--------------------------------------------------+
1 row in set (0.00 sec)

For the sake of reference, I tested MySQL 5.0.45 and PHP 5.2.3. YMMV.

Technorati Search Technorati Search Irish Bloggers

Comments

No comments.

Post a comment

All form information is optional, but it’s a good idea to fill in your name and email address if you want me to take your comment seriously.

Spammers, don’t bother posting crap down here. The site is set up so that legitimate search engines (Google, for instance) won’t index pages with comments on them. Posting crud here only means you’re wasting my time and patience. Shoo!

Real names, please. Please include!
Won’t be displayed. Please include!
Displayed, if present.