Php 时差收入计算

Php 时差收入计算,php,Php,我有下面的代码,它显示了两个日期之间的差异。现在我想根据这一差异计算我的收入,基本费率为每小时8美元,但我还没有做到这一点,因为dateTime返回差异的方式。有什么想法吗 $lastUpdate = new DateTime($lastUpdate['lastUpdate']); $currentTime = new DateTime("2013-03-24 19:45:55"); $interval = $lastUpdate->diff($currentTime); echo "di

我有下面的代码,它显示了两个日期之间的差异。现在我想根据这一差异计算我的收入,基本费率为每小时8美元,但我还没有做到这一点,因为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";

从diff获得总秒数,将其除以3600,再乘以您的速率

试着这样做:

下面是代码的一部分,让您了解:

  //...
  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; 
  } 
  //...

从diff获得总秒数,将其除以3600,再乘以您的速率

试着这样做:

下面是代码的一部分,让您了解:

  //...
  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; 
  } 
  //...

解决方案是转换成秒,然后乘以
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);

解决方案是转换成秒,然后乘以
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);

你为什么不把时间用于你的目的

$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";

你为什么不把时间用于你的目的

$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";

如果希望输出美观,则需要保留现有内容并计算一些新变量

$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; 

如果希望输出美观,则需要保留现有内容并计算一些新变量

$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; 

那么,告诉我们DateInterval的返回方式有什么问题吗?你没有很好地解释,我有一个类似于mysql的解决方案,我有两个日期之间的差异,它根据每秒的收入更新一个字段,我想用php做同样的事情。我发现了差异,但我不知道如何计算收入/按照mysql处理的方式设置日期的格式。($interval->$interval->format(“%a”)/$hoursPerDay*$baseRate)+$interval->$interval->format(“%h”)*$baseRate;那么,告诉我们DateInterval的返回方式有什么问题吗?你没有很好地解释,我有一个类似于mysql的解决方案,我有两个日期之间的差异,它根据每秒的收入更新一个字段,我想用php做同样的事情。我发现了差异,但我不知道如何计算收入/按照mysql处理的方式设置日期的格式。($interval->$interval->format(“%a”)/$hoursPerDay*$baseRate)+$interval->$interval->format(“%h”)*$baseRate;