【发布时间】:2012-08-26 16:43:48
【问题描述】:
我在检查时间范围时遇到问题。
例子
<?php
$starthour = '08'; //Starting work hour
$finishhour = '23'; //Finishing work hour
$extrapaidhours = 0; //Counting extrapaying hours
if($starthour <= 5) {
if($starthour==5) { $extrapaidhours += 5; }
else { $extrapaidhours += 5 - $starthour; }
}
if($starthour > 22) {
$extrapaidhours += 24 - $starthour;
}
if($finishhour <= 5) {
if($finishhour==5) { $extrapaidhours += 5; }
else { $extrapaidhours += 5 - $finishhour; }
}
if($finishhour > 22) {
$extrapaidhours += 24 - $finishhour;
}
echo $extrapaidhours;
我有点堆叠,我认为我的逻辑做错了。 我正在尝试获取 22-05 之间的时间。
也许有人从 21:00 到 05:00 开始工作,并且他在这个范围内有额外的工作时间,即从 22:00 到 05:00 的 7 个小时。
我该如何处理。谢谢大家。
【问题讨论】:
-
有
DateIntervalclass(在 PHP 5.3 及更高版本中)。 -
有一个关于 DateInterval 的小文档,用谷歌搜索了一些东西,但找不到合适的东西。
-
其实,
DateTime::diff。它给你的是一个DateInterval对象。