charlie - 22 days ago 4x
PHP Question

# convert number of months to the number of years and months

I am using a for loop to display numbers 1-60:

``````for(\$i=0; \$i<=60; \$i++)
``````

within the loop, i want to be able to show the number of years and months. for example:

``````1 month
2 months
3 month
...
1 year
1 year 1 month
1 year 2 month
``````

and so on...

i tried this:

``````if(!is_float(\$i/12)) {
\$years = \$i/12;
} else {
\$years = 'no';
}
``````

this shows 1 on the 12 month, 2 on 24 months but not the in between

I have used the following solution to complete my code with help from @scaisEdge

``````for(\$i=0; \$i<=60; \$i++) {
if(!is_float(\$i/12)) {
\$years = floor(\$i / 12).' Year';
\$years = \$years.(\$years > 1 ? 's' : '');
if(\$years == 0) {
\$years = '';
}
}
\$months = ' '.(\$i % 12).' Month';
if(\$months == 0 or \$months > 1) {
\$months = \$months.'s';
}

\$display = \$years.''.\$months;
echo '<option value="'.\$i.'"';
if(\$result["warrenty"] == \$i) {
echo 'selected="selected"';
}
echo '>'.\$display.'</option>';
}
``````