2014-05-19 32 views
-3

我計算了加入日期到當前日期的員工體驗(月)。我want every **last four months in a year** (eg 9-12 and 21-24 and 33-36 etc....) employee經驗已經顯示爲不同的顏色。(PHP代碼)每年最近四個月的顏色與其他月份的顏色不同php

First year calculation has no problem after the years the conditions is not satisfy the `above criteria.` 
This is my code but i need satisfy above criteria. 

if($months % 9 == 0 || $months % 10 == 0 || $months % 11 == 0 || $months % 12 == 0) 
    { 
    <span style="color:green;"><?php echo $u_tot_exp;?><span> 
    } 
    else 
    { 
    <span style="color:black;"><?php echo $u_tot_exp;?><span> 
    } 
+0

什麼問題/什麼是不工作? –

+0

你的問題在哪裏? – iCaramba

回答

1

的問題是你的取模運算%的誤解。您正在使用$months % 11 == 0來表示「如果這個月是一年中的第十一個月」,但這不是這個意思。它實際上意味着「如果這個月是第一個月之後的十一倍」。因此,這意味着11月(第11個月),然後10月下一個(22日),然後9月(第33個)。

效果乘以% 10% 9。如果我們假設第一年是2014年,它會選擇在接下來的幾個月:

2014: September, October, November, December 
2015: June, August, October, December 
2016: March, June, September, December 

%操作者的作品通過計算餘下當左邊的數字是由右側的數除以。由於我們在幾年正在努力,我們總是需要由12然後你要檢查是否遺留下來的數量是3(即九月)之間0(即十二月)來劃分。

$monthsToGo = $months % 12; // months remaining in the year 
if ($monthsToGo >= 3) { // i.e. after September 
    echo "<span style=\"color:green;\">$u_tot_exp<span>"; 
} else { 
    echo "<span style=\"color:black;\">$u_tot_exp<span>"; 
} 

請注意,我還修復了輸出HTML的代碼。

0
**Finally i have get the result using this code.** 

<?php 
$currentDate = date("d-m-Y"); 
    // joining_date is name of field in DB. 
    $date1 = date_create("".$u_joining_date.""); // 
    $date2 = date_create("".$currentDate.""); 
    $diff12 = date_diff($date2, $date1); 
    $hub_days = $diff12->days; 
    $months = $diff12->m; 
    $years = $diff12->y; 
    $tot_months = (($years * 12) + $months); 
    //$monthsToGo = $months % 12; 
    // months remaining in the year 
    //$monthsmod = $monthsToGo % 10; 
if ($tot_months != 0) 
{ 
if($months % 9 == 0 || $months % 10 == 0 || $months % 11 == 0 || $months % 12 == 0) 
{ 
?> 
<span style="color:green;"><?php echo $tot_months;?>month(s)<span> 
<?php 
} 
else 
{ 
?> 
<span style="color:black;"><?php echo $tot_months;?>month(s)<span> 
<?php 
} 
} 
?> 
相關問題