I am currently working on an application that must take a user's birthdate, and the date of a particular event, and use them to calculate the user's age when the event occurred.
MySQL has a built in function called DATEDIFF() that returns the number of days between any two dates. So to calculate the user's age when the event occured, we just use DATEDIFF() to find the number of days between event_date, and user_birthdate, and then divide by 365 and throw out the remainder, right?
Well, thats what I thought anyway. But in testing, it became obvious that if the event month/day was only a few days before the user's birthdate month/day, the answer was always one year more than it should have been.
Enter leap year. MySQL is smart enough to factor in leap years, so some years have 366 days between the same two calendar days. So to calculate age correctly, just divide by 365.25 instead of 365!
FLOOR(DATEDIFF(event_date,user_birthdate)/365.25) AS user_age_at_event_calc
Sometimes it is the simplest things that are the hardest!
Hello! My name is Eric Weik. I am a computer scientist, photographer, musician, and occasional blogger. New Rivers Digital is my software consulting business. I am dedicated to using open source software and open data standards for Web development and applications integration. In particular, I am an ardent Drupal fan and specialize in Drupal module development, theming, and data architecture integration.
Reply to comment
Calculating Age as of a Date: How Leap Year Complicates Things
In:I am currently working on an application that must take a user's birthdate, and the date of a particular event, and use them to calculate the user's age when the event occurred.
MySQL has a built in function called DATEDIFF() that returns the number of days between any two dates. So to calculate the user's age when the event occured, we just use DATEDIFF() to find the number of days between event_date, and user_birthdate, and then divide by 365 and throw out the remainder, right?
Well, thats what I thought anyway. But in testing, it became obvious that if the event month/day was only a few days before the user's birthdate month/day, the answer was always one year more than it should have been.
Enter leap year. MySQL is smart enough to factor in leap years, so some years have 366 days between the same two calendar days. So to calculate age correctly, just divide by 365.25 instead of 365!
FLOOR(DATEDIFF(event_date,user_birthdate)/365.25) AS user_age_at_event_calcSometimes it is the simplest things that are the hardest!
Reply
About
Hello! My name is Eric Weik. I am a computer scientist, photographer, musician, and occasional blogger. New Rivers Digital is my software consulting business. I am dedicated to using open source software and open data standards for Web development and applications integration. In particular, I am an ardent Drupal fan and specialize in Drupal module development, theming, and data architecture integration.
Contact Details
Lancaster, VA 22503
Content Tags
Abstract B&W Celestial Clouds Drupal 6 Drupal Recipes Drupal Sites Estuary Etsy Government Grasses HDR Long Exposure Macro New Rivers Digital Orton Photoblog Photo Expedition Photoset Renderblog RGB Sketchbook Snow Storm Structure Synth Sunflow Sunset Theming Webform.module Wide Angle Zen Zen ThemingRecent comments
Popular content
Today's:
All time:
Activity Stream
Sun, 01/29/2012 - 22:31
Mon, 01/16/2012 - 10:43
Fri, 01/06/2012 - 07:54
Powered by Drupal & Genesis | Valid XHTML 1.0 Strict |
RSS Feed
© 2010 New Rivers Digital | PO Box 784 | Lancaster, Virginia 22503 | +1-804-577-8526 | Contact Form