我有一些来自数据库的带有时间段的信息数组,以及一些预定义的时间框架。下面是一个示例:
预定义的时间范围是:12 6am 6 6am、6 6am 9 6am、9 6am 5 5pm、5 5pm 8 5pm、8 5pm 12 6am我的数据库时间段可能是:4:30 5pm到8:30 5pm
如果我的数据库时间跨度超过一个或多个时间范围,我需要将其拆分成块。在这种情况下,我需要将4:30-8:30拆分为4:30-5 8pm、5 8pm 8 8pm、8-8:30 8pm,现在有3个数组,而不是1个。根据时间范围,我还需要对这些数组执行进一步的操作。
我想知道的是,我提出的确定我的数组属于哪个时间段的方法a)有任何意义,b)是解决问题的最佳方法。
以下是我的PHP的一小部分,但您可以看到我的进程:
// First, a function to determine if a timeframe is between 2 dates, which I found on another SO post:
function date_between($from, $to, $date = 'now') {
$date = is_int($date) ? $date : strtotime($date);
$from = is_int($from) ? $from : strtotime($from);
$to = is_int($to) ? $to : strtotime($to);
return ($date > $from) && ($date < $to);
}
$st = $row['Start'];
$end = $row['End'];
$ot_f = date_create_from_format('M j Y H:i:s:uA', $st);
$ot_e = date_create_from_format('M j Y H:i:s:uA', $end);
$timestart = strtotime(date_format($ot_f, 'Y-m-d H:i')); //Start date, formatted
$timestop = strtotime(date_format($ot_e, 'Y-m-d H:i')); //End date, formatted
$timestump = strtotime(date_format($ot_f, 'Y-m-d')); //I take the Y-m-d from the start date, and I will append the assigned timeframes onto them
$six = $timestump + (360 * 60); //'6:00', so this becomes Y-m-d 06:00;
$nine = $timestump + (540 * 60); //'9:00'
$seventeen = $timestump + (1020 * 60); //'17:00'
$twenty = $timestump + (1200 * 60); //'20:00'
$midnite = $timestump + (1440 * 60); //'00:00'
// Start comparing here
if(date_between($six, $nine, $timestart)){ // Does the timeframe start between 6am and 9am?
if(date_between($six, $nine, $timestop)){ // Does the timeframe end between 6 and 9am?
// If so, define timeframe. It starts and ends in the same timeframe, so we only have one
$row['Start'] = $timestart;
$row['End'] = $timestop;
$row['Timeframe'] = '6-9am';
else if(date_between($nine, $seventeen, $timestop)){
// If so, split the arrays, because now we are talking about multiple timeframes
$row2 = array();
$row['Start'] = $timestart;
$row['End'] = $nine;
$row['Timeframe'] = '6-9am';
$row2['Start'] = $nine;
$row2['End'] = $timestop;
$row2['Timeframe'] = '9am-5pm';
} // Continue from here
} else if(date_between($nine, $seventeen, $timestart)){
} else if(date_between($seventeen, $twenty, $timestart)){
} else {
}我的代码看起来真的很笨拙,这甚至比我写的第一个版本还要简化。我这样做对吗?我能让我的代码变得更好吗?
发布于 2015-12-07 05:55:53
进一步扩展这个答案,我可以在这段代码中看到的一个主要缺陷是可扩展性。如果我想添加另一个时间段来检查,我将不得不添加更多的条件语句和更多的变量。
如果您有一个已设置的条件:(x -> y)是否介于(a -> b)之间,并且您有多个实例,或者两者都有,那么您希望在多个实例上使用循环,并对该循环应用相同的条件。
在本例中,您有一个时间段($timestart,$timestop)和多个时间段,因此您将遍历这些时间段,以查看您的时间段是否适合以及在哪里适合。
首先,我不会将所有的时间都作为变量列出,而是将它们放在一个数组中。这使得它们可以循环,还允许您定义每个元素的起点和终点,而不是编写越来越多的条件来确定它是否在$six和$nine之间。
我建议将时间范围放入一个数组中,如下所示:
$timeframes = array (
array(
"start" = "00:00",
"finish" = "06:00"
),
array(
"start" = "06:00",
"finish" = "09:00"
),
...
);然后,您可以使用此数组来执行date_between检查,该数组的每个元素都将包含要根据当前函数进行检查的开始和结束时间。
foreach ($timeframes as $timeframe) {
$check_start = $timeframe['start'];
$check_finish = $timeframe['finish'];
if (date_between($check_start, $check_finish, $timestart)) {
// it's a match
elseif (date_between($check_start, $check_finish, $timestop)) {
// it's a match
} else {
// it's not a match
}
}但是,您可以通过将$timestart 和 $timestop参数都传递给date_between函数来避免执行额外的elseif。
https://stackoverflow.com/questions/34123132
复制相似问题