문제

I have a number of months (say 38 months).

I need to know how to convert this 38 value to say, 3 years and 2 months.

This is what I have so far:

$months = 38;
$years = $months / 12;

$years is equal to 3.1666666666667. I am wondering how to be left with simply 3 years, and then the 2 months left over?

도움이 되었습니까?

해결책

You could calculate the time with the mod operator

$months = 38;
$years = floor($months / 12);
$resMonths = $months % 12;

다른 팁

You can do this.

$months = 38;
$years = floor($months / 12);

//Use modulo to get the remainder
$months = $months % 12;

echo $years . " years and " . $months . " months";

The following code will output “3 years and 2 months”:

$months = 38;
print(floor($months/12)." years and ".($months%12)." months");
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top