Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:如何检查一个UNIX时间戳范围是否与任何UNIX时间戳范围重叠?_Php_Date_Datetime_Time_Unix Timestamp - Fatal编程技术网

PHP:如何检查一个UNIX时间戳范围是否与任何UNIX时间戳范围重叠?

PHP:如何检查一个UNIX时间戳范围是否与任何UNIX时间戳范围重叠?,php,date,datetime,time,unix-timestamp,Php,Date,Datetime,Time,Unix Timestamp,如何在PHP中检查一个UNIX时间戳范围是否与另一个UNIX时间戳范围重叠 我正在开发一个应用程序,它接受未来的预订。但是,每个时段只允许一(1)次预订 示例: X先生预订了从上午10:00到下午12:00(中午)的资源。稍后,Y女士想从上午8:00到晚上11:00预订相同的资源。我的申请应该拒绝Y女士的预约,因为它与X先生先前的预约重叠 我将现有保留的开始和结束时间存储在UNIX时间戳(整数)中,但如果需要,我可以将它们转换为以下格式“yyy-mm-dd-hh:mm:ss”,反之亦然 我不

如何在PHP中检查一个UNIX时间戳范围是否与另一个UNIX时间戳范围重叠

我正在开发一个应用程序,它接受未来的预订。但是,每个时段只允许一(1)次预订


示例

X先生预订了从上午10:00到下午12:00(中午)的资源。稍后,Y女士想从上午8:00到晚上11:00预订相同的资源。我的申请应该拒绝Y女士的预约,因为它与X先生先前的预约重叠


我将现有保留的开始和结束时间存储在UNIX时间戳(整数)中,但如果需要,我可以将它们转换为以下格式“yyy-mm-dd-hh:mm:ss”,反之亦然

我不明白如何解决这个问题。如果我用所有现有的预订开始时间检查新的开始时间,并以类似的方式检查新的结束时间,那么逻辑将有许多
If
语句,使应用程序变慢

请您帮助我在不使用大量服务器资源的情况下高效地解决此问题

非常感谢你的帮助

谢谢你的介绍 换句话说,您需要对特定资源的所有保留间隔(UNIX时间戳)进行比较,以确定新保留是否有效(在新保留的域内)

第一步 首先,类似的SQL查询可能会有所帮助。虽然像
ANY
ALL
NOT
EXISTS
等关键词似乎很吸引人,但在发生计划冲突时(根据用户界面),您需要决定需要多少信息。此查询提供了一个机会,可以通过前瞻性预测提取有关潜在预订的最大信息量(PHP等)

// A query like this might help. It's not perfect, but you get the idea.
// This query looks for ALL potential conflicts, starting and ending.

    $sql = "SELECT DISTINCT `t1`.`startTime`, `t1`.`endTime`
              FROM `reservations` AS `t1`
             INNER JOIN `resources` AS `t2`
                ON `t1`.`resourceId` = `t2`.`resourceId`
             WHERE `t2`.`resourceId` = :resourceId
               AND (`t1`.`startTime` BETWEEN :minTime1 AND :maxTime1)
                OR (`t1`.`endTime` BETWEEN :minTime2 AND :maxTime2)
             ORDER BY `t1`.`startTime` ASC";
可能。这将给您留下一个多维数组。以下逻辑允许您获得一份报告,详细说明无法进行预订的原因。由您在另一个模块中解释报告

步骤2 将解决方案概括为
保留
类的方法。根据您的RDBMS,您可以在SQL中执行类似的操作。尽管如此,它可能会更不具体,您以后可能需要粒度。您可以将JSON格式的报告发送到JavaScript前端(只是需要考虑一下)

步骤4 使用预先准备好的语句在
保留
表上创建一种方法。一般来说

private function getReservationTimes($resourceId, $minTime, $maxTime) 
{
    $sql = "SELECT DISTINCT `t1`.`startTime`, `t1`.`endTime`
              FROM `reservations` AS `t1`
             INNER JOIN `resources` AS `t2`
                ON `t1`.`resourceId` = `t2`.`resourceId`
             WHERE `t2`.`resourceId` = :resourceId
               AND (`t1`.`startTime` BETWEEN :minTime1 AND :maxTime1)
                OR (`t1`.`endTime` BETWEEN :minTime2 AND :maxTime2)
             ORDER BY `t1`.`startTime` ASC";

    $stmt = $this->db->prepare($sql);
    $stmt->bindParam(:resourceId , $resourceId);
    $stmt->bindParam(:minTime1 , $minTime);
    $stmt->bindParam(:maxTime1 , $maxTime);
    $stmt->bindParam(:minTime2 , $minTime);
    $stmt->bindParam(:maxTime2 , $maxTime);
    $stmt->execute();
    return $stmt->fetchAll();
}
步骤5 为整个流程创建一个公共方法(接口)

public function isOpen($minTime, $maxTime)
{
    $periods = $this->getReservationTimes($this->resource->getResourceId(), $minTime, $maxTime);

    if (empty($periods)) {
        return true;  //You may reserve the resource during the time period.
    }

    return $this->checkPeriods($periods, $this->start, $this->end));  
}
步骤6 将关注点分开

为保留的实际项目创建类层次结构

abstact class Product
{

}

class Resource extends Product implements Reservable  //Or, something ...
{
    private $resourceId;

   //etc ....
}
为保留创建类层次结构

abstract class Interval
{
    private $start;
    private $end;

    public function __construct($start, $end)
    {
        $this->start = $start;
        $this->end   = $end;
    }
}

class Reservation extends Interval
{
    private $db;
    private $resource;

    public function __construct(PDO $pdo, Reservable $resource, $reqStartTime, $reqEndTime)
    {
        parent::__construct($reqStartTime, $reqEndTime);
        $this->db = $pdo;
        $this->resource = $resource;
    }
}
步骤7 快跑

实例化
Reservation
对象时,请至少提供一个
Reservable
对象、请求的开始时间和请求的结束时间(在本例中为UNIX时间戳)


所以有效的预约应始终为start1EndB,则为True如果EndASELECT。。。其中开始<:结束和结束>:开始(如果找到行-重叠)。有关详细信息,请参阅。@shudder啊,但是冲突的原因还不清楚。@Gerrit0有点像我的答案。这将只检查开始时间,如果它是否在数组中。如果它在数组中,它将停止执行,并说您不能保留此资源。就像我给出的示例一样,开始时间是上午8点,保留资源的开始时间是上午10点。基于在_array()中使用的函数,它将为false,因为保留的时间戳中不存在上午8点,并且它将允许我保留不应该保留的资源,因为上午8点到上午11点的日期范围确实与现有保留重叠。结束时间检查也是一样。好的,现在,这个怎么样。好的,这个怎么样。我为你做了一个检查预订的功能。它可能会工作。如果($newStartTime<$startTime)和($newEndTime<$endTime),它没有通过以下if语句中的测试用例{//执行指针将进入此段,它将阻止用户预订此资源。//比方说,我们将允许用户预订,如果上述条件为真//那么对于newEndTime=2017-2-28 15:00:00//的情况,将满足此条件并允许用户预订。但实际上不应}
abstact class Product
{

}

class Resource extends Product implements Reservable  //Or, something ...
{
    private $resourceId;

   //etc ....
}
abstract class Interval
{
    private $start;
    private $end;

    public function __construct($start, $end)
    {
        $this->start = $start;
        $this->end   = $end;
    }
}

class Reservation extends Interval
{
    private $db;
    private $resource;

    public function __construct(PDO $pdo, Reservable $resource, $reqStartTime, $reqEndTime)
    {
        parent::__construct($reqStartTime, $reqEndTime);
        $this->db = $pdo;
        $this->resource = $resource;
    }
}
try
{
    $day          = 84600;                   // Seconds per day.
    $future       = $day * 90;               // Application specific.

    //User requested times.
    $reqStartTime = 1488394687 + $day;       // Tomorrow.
    $reqEndTime   = 1488394687 + ($day * 2); // Two day duration.

    //Search constraints.
    $minTime      = time();                   // Today. Right now.
    $maxTime      = 1488394687 + $future;     // 90 day look ahead.

    $reservation  = new Reservation($pdo, $resourceObj, $reqStartTime, $reqEndTime);
    $availability = $reservation->isOpen($minTime, $maxTime);

    if($availability === true){
        $reservation->confirm();
    } else {
        //Have some other object deal with the report
        $reporter = new Reporter($availability);
        $reporter->analyzeReport();
        //Have some other object update the view, etc ...
    }

}
catch(Exception $e)
{
    //Handle it.
}