I want to convert Years (with Months in decimal) to Months only via PHP
Like
1.2 Years (1 Year 2 Months) to 14 Months
1.0 Year to 12 Months
I have written the below code that may be having issues so want to correct it.
$yearwithdecimal = "1.2";
$yearwithdecimal = (float)$yearwithdecimal;
if (is_float($yearwithdecimal)) {
$yearsArray = explode(".",$yearwithdecimal);
$year_months = $yearsArray[0]*12;
$more_months = $yearsArray[1];
$total_months = $year_months + $more_months;
}else{
$total_months = $yearwithdecimal*12;
}
echo $total_months; die;
// Output is 14
Thanks in Advance!
3
Answers
You can use
round()
in php.*Not quite accurate.
Output:
Suppose your field the first part represents num of years and the second part represents num of months.
Take the field as a string, then explode it by
.
Demo: https://3v4l.org/VhOfV