Convert decimal year to year, months and days


/**
*  Function : year2monthsNdays
*  Arguments : $years float value (Ex: 42.5, 30.89, 52.00, etc..)
*  Returns : array (year,month,days as keys)
*  Description: Converts a given float value of years to years, months and days
*/
function year2monthsNdays($years)
{
    $array    = explode(".",$years);
    $year    = $array[0];
    $month    = ($array[1]>9) ? $array[1]/100 : $array[1]/10;
    if ($month) {
        $days        = round($month*365,2);
        $daysArray    = explode(".",$days);
        $months        = round($daysArray[0]/30,2);
        $monthArray    = explode(".",$months);
        $monthInt    = $monthArray[0];
        $daysInt    = round($monthArray[1]*30/100,1);
    }
    $returnArray['year']    = $year;
    $returnArray['month']    = $monthInt;
    $returnArray['days']    = $daysInt;
    return $returnArray;
}
Advertisements
Convert decimal year to year, months and days

One thought on “Convert decimal year to year, months and days

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s