我怎样才能在 PHP 中正确地获得这个特定部门的其余部分?
How can I properly get the rest of this specific division in PHP?
我有这个功能
public function handleaddMinutes(int $addedMinutes): void {
$newMinutes = $this->getMinutes() + $addedMinutes;
if($newMinutes > 60) {
$hours = round($newMinutes / 60, 0, PHP_ROUND_HALF_DOWN);
$this->addHour($hours);
// TODO
}
$this->addMinutes($newMinutes); //TODO
}
如果分钟超过60
,则应增加一个小时,其余部分应按分钟添加。所以,假设我用 125
分钟调用函数。该函数现在应该使用 $this->addHour()
添加 2 hours
和 $this->addMinutes()
添加 25 minutes
。
我怎样才能做到这一点?我尝试使用下面的代码,这可能会增加小时数,但我怎么能将剩余的时间添加为分钟数呢?
模运算符 %
将为您提供剩余时间:
public function handleaddMinutes(int $addedMinutes): void {
$newMinutes = $this->getMinutes() + $addedMinutes;
if($newMinutes > 60) {
$hours = round($newMinutes / 60, 0, PHP_ROUND_HALF_DOWN);
$this->addHour($hours);
// TODO
}
$this->addMinutes($newMinutes % 60);
}
我有这个功能
public function handleaddMinutes(int $addedMinutes): void {
$newMinutes = $this->getMinutes() + $addedMinutes;
if($newMinutes > 60) {
$hours = round($newMinutes / 60, 0, PHP_ROUND_HALF_DOWN);
$this->addHour($hours);
// TODO
}
$this->addMinutes($newMinutes); //TODO
}
如果分钟超过60
,则应增加一个小时,其余部分应按分钟添加。所以,假设我用 125
分钟调用函数。该函数现在应该使用 $this->addHour()
添加 2 hours
和 $this->addMinutes()
添加 25 minutes
。
我怎样才能做到这一点?我尝试使用下面的代码,这可能会增加小时数,但我怎么能将剩余的时间添加为分钟数呢?
模运算符 %
将为您提供剩余时间:
public function handleaddMinutes(int $addedMinutes): void {
$newMinutes = $this->getMinutes() + $addedMinutes;
if($newMinutes > 60) {
$hours = round($newMinutes / 60, 0, PHP_ROUND_HALF_DOWN);
$this->addHour($hours);
// TODO
}
$this->addMinutes($newMinutes % 60);
}