首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用PHP确定一个时间段是否在一个或多个时间范围内

如何使用PHP确定一个时间段是否在一个或多个时间范围内
EN

Stack Overflow用户
提问于 2015-12-07 05:41:03
回答 1查看 69关注 0票数 1

我有一些来自数据库的带有时间段的信息数组,以及一些预定义的时间框架。下面是一个示例:

预定义的时间范围是: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的一小部分,但您可以看到我的进程:

代码语言:javascript
复制
// 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 {

}

我的代码看起来真的很笨拙,这甚至比我写的第一个版本还要简化。我这样做对吗?我能让我的代码变得更好吗?

EN

回答 1

Stack Overflow用户

发布于 2015-12-07 05:55:53

进一步扩展这个答案,我可以在这段代码中看到的一个主要缺陷是可扩展性。如果我想添加另一个时间段来检查,我将不得不添加更多的条件语句和更多的变量。

如果您有一个已设置的条件:(x -> y)是否介于(a -> b)之间,并且您有多个实例,或者两者都有,那么您希望在多个实例上使用循环,并对该循环应用相同的条件。

在本例中,您有一个时间段($timestart$timestop)和多个时间段,因此您将遍历这些时间段,以查看您的时间段是否适合以及在哪里适合。

首先,我不会将所有的时间都作为变量列出,而是将它们放在一个数组中。这使得它们可以循环,还允许您定义每个元素的起点和终点,而不是编写越来越多的条件来确定它是否在$six$nine之间。

我建议将时间范围放入一个数组中,如下所示:

代码语言:javascript
复制
$timeframes = array (
    array(
        "start" = "00:00",
        "finish" = "06:00"
    ),
    array(
        "start" = "06:00",
        "finish" = "09:00"
    ),
    ...
);

然后,您可以使用此数组来执行date_between检查,该数组的每个元素都将包含要根据当前函数进行检查的开始和结束时间。

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34123132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档