我有一个开始时间和结束时间,它们总是在精确的小时开始或结束:00或:15,:30,:45。
例如:
00:30 21-05-2012 // startdate and time
18:15 22-05-2012 // enddate and time (+1 day)
如果时间落入晚上或晚上,我必须收取额外的百分比。
所以我有 2 个数组,一个用于晚上,一个用于夜间,如下所示。
$eveningHours = array(
'18:00',
'18:15',
'18:30',
// etcetera
'00:00'
);
上面的数组基本上涵盖了 18:00 - 00:00 的时间,因为这些时间落入了晚上的时间。
夜间时间也是如此,它们涵盖 00:00 - 07:00 的时间。
由于开始时间和结束时间总是精确地落在 :00 或 :15、:30、:45。我在开始日期时间和结束日期时间之间以 15 分钟的间隔运行一个循环,然后在每个间隔上检查当前间隔时间是否为夜间/晚上小时,然后 Ii 将 +15 添加到晚上/夜间小时计数器,最后我将其除以 60 得到总小时数。
但不知何故,它总是有 15 分钟到很多或 15 分钟到很少......我几乎尝试了所有方法,但无法使其正常工作。
因此,例如,开始时间是 00:30,它已经在第一个间隔上增加了 15 分钟:
00:30 (计数器: +15(00:45 (计数器: +15(
总共 30分钟,但如果你从 00:30 到 00:45 工作,它应该只有 15 分钟。所以我认为这就是出错的地方。我只需要一种方法来修复它。我已经尝试了很多事情,从 -15 分钟开始计数器,或者检查开始或结束时间是否在小时范围内,然后将计数器设置为 -15 或 + 15 分钟。但这也出了问题。
编辑 这是循环的基础:
$start_time = mktime($_POST['worked_start_hour'], $_POST['worked_start_min'], 0, date("m"), date("d", strtotime("+1 days")), date("Y")); // +1 day for testing purpose
$end_time = mktime($_POST['worked_end_hour'], $_POST['worked_end_min'], 0, date("m"), date("d", strtotime("+2 days")), date("Y")); // +2 days for testing purpose
$date['start_h_i'] = date("Y-m-d H:i", $start_time); # Start date: yyyy-mm-dd hours:minutes
$date['end_h_i'] = date("Y-m-d H:i", $end_time); # End date: yyyy-mm-dd hours:minutes
$counter['eveningMinutes'] = 0;
$counter['nightMinutes'] = 0;
$eveningHours = array(
'18:00',
'18:15',
'18:30',
'18:45',
'19:00',
'19:15',
'19:30',
'19:45',
'20:00',
'20:15',
'20:30',
'20:45',
'21:00',
'21:15',
'21:30',
'21:45',
'22:00',
'22:15',
'22:30',
'22:45',
'23:00',
'23:15',
'23:30',
'23:45',
'00:00'
);
$nightHours = array(
'00:00',
'00:15',
'00:30',
'00:45',
'01:00',
'01:15',
'01:30',
'01:45',
'02:00',
'02:15',
'02:30',
'02:45',
'03:00',
'03:15',
'03:30',
'03:45',
'04:00',
'04:15',
'04:30',
'04:45',
'05:00',
'05:15',
'05:30',
'05:45',
'06:00',
'06:15',
'06:30',
'06:45',
'07:00'
);
while (strtotime($date['start_h_i']) <= strtotime($date['end_h_i'])) {
if (in_array(date("H:i", strtotime($date['start_h_i'])), $eveningHours)) {
$counter['eveningMinutes'] = $counter['eveningMinutes'] + 15;
}
if (in_array(date("H:i", strtotime($date['start_h_i'])), $nightHours)) {
$counter['nightMinutes'] = $counter['nightMinutes'] + 15;
}
$date['start_h_i'] = date ("Y-m-d H:i", strtotime("+15 minutes", strtotime($date['start_h_i']))); // 15 minutes interval
} // end while
我希望有人能在这里发光,谢谢!
任何人?
更改此行:
while (strtotime($date['start_h_i']) <= strtotime($date['end_h_i'])) {
对此:
while (strtotime($date['start_h_i']) < strtotime($date['end_h_i'])) {
您遇到的问题是,当您已经完成时间计算时,您正在更新小时数。所以使用你自己的00:30 - 00:45
的例子:
- 第一次循环:00:30 <= 00:45,因此添加 15 分钟
- 循环第二次:00:45 <= 00:45,因此添加 15 分钟
通过将<=
更改为<
,您可以删除第二个操作:00:45 !<00:45,因此不添加任何内容。