2014-01-09 39 views
1

我有几个月(比如38个月)。在PHP中划分并保留余额

我需要知道如何将这个38值转换为3年和2个月。

这是我到目前为止有:

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

$几年等于3.1666666666667。我想知道如何留下3年,然后剩下2个月?

回答

4

你可以用mod运算符

$months = 38; 
$years = floor($months/12); 
$resMonths = $months % 12; 
计算时间
2

下面的代码将输出“3年2个月”:

$months = 38; 
print(floor($months/12)." years and ".($months%12)." months"); 
2

你可以做到这一点。

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

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

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