Posts

Showing posts with the label dob

Efficient Age From Date of Birth Calculation - PHP edition

Well thought I would have a look at the same set of methods in PHP as I did in MS SQL.  The order of efficiency did change somewhat, which was certainly interesting to me.  Below is my simple code. $timeformatted = date("Y-m-d",strtotime('20th March 1954')); //Start test 1 $time_start = microtime(true); $datetime1 = new DateTime($timeformatted); $datetime2 = new DateTime("now"); $interval = date_diff($datetime1, $datetime2); echo " Test1 ".$interval->y ; $time_end = microtime(true); $time = ($time_end - $time_start)*100; echo " $time ms "; //start test2 $time_start2 = microtime(true); $japDate1 = str_replace("-","",$timeformatted); $japDate2 = date("Ymd",time()); $answer = floor(($japDate2 - $japDate1)/10000); echo " Test2  $answer "; $time_end2 = microtime(true); $time2 = ($time_end2 - $time_start2)*100; echo " $time2 ms "; //start test 3 $time_start...

Efficient Age From Date of Birth Calculation

I have been playing around with MS SQL and I was wondering what would be the most efficient method to gather a users age. Calculations involving dates should always be considered from their most tricky state, if you consider that you might need to take into account UTC time, leap years, time zones it all starts to add up to complexity that you can too easily trip up over.  So my first thought when someone asks me to calculate the age from a DOB field is to "cheat" and use the built in languages tools. Most languages have some form of datediff function that can be used to calculate the time between two dates.  However is this going to be the most efficient method? I thought I would test a couple of methods on MS SQL to see the performance changes.  All tests were performed on a low spec laptop on MS SQL Express on a set of 100 random date of births First method SELECT floor(datediff(day,[dob],current_timestamp) / 365.25) Well this is nice and succinct but does ...