2013-03-24 110 views
1

我有以下代碼,它顯示了兩個日期之間的差異。現在我想根據這種差異來計算我的收入,基本費率爲每小時8美元,但由於dateTime返回給我的差異,我還沒有管理過。任何ideeas?datetime差異收入計算

$lastUpdate = new DateTime($lastUpdate['lastUpdate']); 
$currentTime = new DateTime("2013-03-24 19:45:55"); 
$interval = $lastUpdate->diff($currentTime); 
echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days " . $interval->h . " hours " . $interval->i . " minutes " . $interval->s . " seconds"; 
+0

所以告訴我們關於返回一個DateInterval方式的問題?你不是很好解釋 – 2013-03-24 17:23:46

+0

以及我有一些類似於mysql解決方案,我有兩個日期之間的差異,它更新基於每秒收入的字段,我想在php中做同樣的事情。我發現差異,但我不知道如何計算收入/格式的日期相同的方式MySQL處理它。 – Bogdan 2013-03-24 17:53:57

+0

($ interval - > $ interval-> format('%a')/ $ hoursPerDay * $ baseRate)+ $ interval - > $ interval-> format('%h')* $ baseRate; – 2013-03-24 18:35:56

回答

2

從差異中獲取總秒數,將它們分爲3600和倍數。

嘗試這樣:http://www.php.net/manual/en/dateinterval.format.php#102271

就從這裏的部分代碼給你一個想法:

//... 
    public function to_seconds() 
    { 
    return ($this->y * 365 * 24 * 60 * 60) + 
      ($this->m * 30 * 24 * 60 * 60) + 
      ($this->d * 24 * 60 * 60) + 
      ($this->h * 60 * 60) + 
      ($this->i * 60) + 
      $this->s; 
    } 
    //... 
2

解決辦法是轉換成秒,乘以8/3600

$lastUpdate = new DateTime($lastUpdate['lastUpdate']); 
$currentTime = new DateTime("2013-03-24 19:45:55"); 
$interval = $lastUpdate->diff($currentTime); 
//generate time string 
$timeStr  = ""; 
if($interval->y >0) { $timeStr .= $interval->y ." year" .($interval->y==1 ? " ": "s "); }  
if($interval->m >0) { $timeStr .= $interval->m ." month" .($interval->m==1 ? " ": "s "); }  
if($interval->d >0) { $timeStr .= $interval->d ." day" .($interval->d==1 ? " ": "s "); } 
if($interval->h >0) { $timeStr .= $interval->h ." hour" .($interval->h==1 ? " ": "s "); } 
if($interval->i >0) { $timeStr .= $interval->i ." minute" .($interval->i==1 ? " ": "s "); } 
if($interval->s >0) { $timeStr .= $interval->s ." second" .($interval->s==1 ? " ": "s "); } 
//add up all seconds 
$seconds = ($interval->y * 365* 24 * 60 * 60) 
     + ($interval->m * 30 * 24 * 60 * 60) 
     + ($interval->d * 24 * 60 * 60) 
     + ($interval->h * 60 * 60) 
     + ($interval->i * 60) 
     + $interval->s; 
//multiply by your wage (in seconds) 
$hourly_rate = 8.00; 
$pay   = ($seconds * $hourly_rate)/3600; 
$pay   = round($pay,2,PHP_ROUND_HALF_UP); //round to nearest cent 
//print out the resulting time, rate & cost statement 
printf("Total time of %sat hourly rate of $%.2f equates to $%.2f\n",$timeStr,$hourly_rate,$pay); 
2

爲什麼你不使用strtotime爲您的目的

$lastUpdate = strtotime($lastUpdate['lastUpdate']); 
$currentTime = strtotime("2013-03-24 19:45:55"); 
$interval = ($currentTime - $lastUpdate)/(3600); 
$interval = round($interval,2); 
$income = $interval*8; 
echo $interval."hours"; 
1

如果你想輸出很漂亮,你需要保留你在那裏的東西並計算一些新的變量。

$years = $interval->y * 365/24; // give it that it's not a leap year 
$months = $interval->m * 730; // approximation of how many hours are in a month 
// ... so on 
$grandSummation = $years + $months + $days + $hours + $seconds; 
$finalBaseApproximation = $grandSummation * 8; 
echo "Your income is $" . $finalBaseApproximation;