Categories
Blog

33 years, 7 months

I wonked around writing my own code for this-here site I call ori.nz — something to display my exact age relative to the date of my birth in years, months, and days. Logicking though the process can be quite satisfactory, but so can just having the answer you want easily presented. So here’s what I came up with:

$birthdatetime = array(1983,12,13,19,30);
$year_diff = get_the_date("Y") - $birthdatetime[0];
$month_diff = get_the_date("n") - $birthdatetime[1];
$day_diff  = get_the_date("d") - $birthdatetime[2];
if( $day_diff < 0 ){
        $month_diff--; $day_diff += date("t", (get_the_date('u') - (get_the_date("n") * 86401)));
    } // CALC FROM month b4
    if( $month_diff < 0 ){
        $year_diff--; $month_diff += 12;
    }
    $age_then =
        $year_diff .' years' .
        ($month_diff == 0 ? '' : ', '. $month_diff .' months') .
        ($day_diff == 0 ? '' : ', '. $day_diff .' days');
    echo '' . $age_then . '';

Leave a Reply

Your email address will not be published. Required fields are marked *