Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.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检查时间范围是否在时间范围内_Php_Mysql - Fatal编程技术网

PHP检查时间范围是否在时间范围内

PHP检查时间范围是否在时间范围内,php,mysql,Php,Mysql,我想在数据库中插入一个限定于某人的时间。 例如:彼得13:00-19:00 彼得告诉我们他不能在18:00-22:00工作。(位于数据库中) 因此,无法将时间13:00-19:00插入数据库,因为结束时间(19:00)在18:00-22:00的时间范围内 如何检查这个 我试过几件事,但我不知道怎么做才对 我现在就知道了 $van_besch = '18:00'; $tot_besch = '22:00'; $van = '13:00'; $tot = '19:00'; if (($van_b

我想在数据库中插入一个限定于某人的时间。 例如:彼得13:00-19:00

彼得告诉我们他不能在18:00-22:00工作。(位于数据库中)

因此,无法将时间13:00-19:00插入数据库,因为结束时间(19:00)在18:00-22:00的时间范围内

如何检查这个

我试过几件事,但我不知道怎么做才对

我现在就知道了

$van_besch = '18:00';
$tot_besch = '22:00';

$van = '13:00';
$tot = '19:00';

if (($van_besch >= $van) && ($tot_besch <= $tot)) {
        $error ='<font color="red">Time '.$van_besch.' - '.$tot_besch.' Not available</font>';
    }
$van_besch='18:00';
$tot_besch='22:00';
$van='13:00';
$tot='19:00';

如果($van_besch>=$van)&($tot_besch实际上有两种情况需要检查:

  • $van
    等于或介于
    $van_-besch
    $tot_-besch

  • $tot
    等于或介于
    $van_besch
    $tot_besch

  • 如果
    $van
    $tot
    都在
    $van_-besch
    $tot_-besch
    之间,则这两种情况都是正确的

    此外,您还需要处理跨越一天休息时间的轮班,例如17:00-02:00。另一个问题是您需要处理的是20:00-0:00,因为0:00小于20:00

    因此,我们将实时投影到我们自己的时间格式。 这意味着17:00-02:00将变为17:00-26:00。请注意,我们需要为
    $van_-besch
    -
    $tot_-besch
    $van
    -
    $tot$
    执行此操作

    在代码中,类似于:

    if ($tot < $van){
       $tot = 24:00 - $van + $tot;
    }
    
    if ($tot_besch < $van_besch){
       $tot = 24:00 - $van + $tot;
    }
    
    if (($van >= $van_besch) && ($van <= $tot_besch) ) || (($tot >= $tot_besch) && ($tot <= $tot_besch)){
     ... Peter is not available ... 
    }
    
    if($tot<$van){
    $tot=24:00-$van+$tot;
    }
    如果($tot_besch<$van_besch){
    $tot=24:00-$van+$tot;
    }
    
    如果($van>=$van_-besch)&($van=$tot_-besch)&($tot您可以在SQL查询中确定地处理这个问题。如果您只是在存储时间,最好将其作为int
    0000-2359
    来处理

    所以我们的桌子看起来像这样

    CREATE TABLE off_time (
      id int(12) AUTO_INCREMENT,
      employee int(12) NOT NULL,
      start_time smallInt(4) unsigned NOT NULL,
      end_time smallInt(4) unsigned NOT NULL,
      PRIMARY KEY (id)
    );
    
    我们正在存储开始时间和结束时间,以创建一个不起作用的时间块。显然,在您的代码中,确保这些时间块有意义和开始时间很重要,但如果您不希望时隙可能重叠开始/结束时间,则可以将它们设为。 如果(1300ot.start_时间,1,0)被阻止,则选择ot.id 从业余时间起,作为ot 其中ot.employee=1;
    您将得到一个表,其中每一行都有阻塞时间段的
    id
    ,还有一个
    blocked
    列,其中1表示阻塞,0表示未阻塞。因此,如果任何条目为1,则您知道您的时间被阻塞。这样做也很方便,因为您还可以关联一个原因表,并能够回复原因。
    LEFT JOIN time\u off\u原因为r on r.id=ot.id

    如果您想明确日期,只需存储开始时间和结束时间的
    时间戳
    。比较结果相同,只需使用时间戳即可

    编辑 Marc B.在关于超过午夜标记(即2200-0200)的时间段的评论中提出了一个很好的观点。使用我的方法,您希望确保将这些时间段作为单独的条目分开

    因此
    插入关闭时间(开始时间、结束时间)值(22002400)(00000200)


    这仍然会像预期的那样工作。但是您只需要在块外一次输入两个条目,这可能会很烦人,但仍然比编写额外的代码来补偿逻辑更容易。

    您最好将它们转换并使用
    strotime
    进行比较。类似这样的操作对您很有用:

    $van_besch = '18:00';
    $tot_besch = '22:00';
    
    $van = '13:00';
    $tot = '19:00';
    
    if (strtotime($van) < strtotime($van_besch) || strtotime($tot ) > strtotime($tot_besch)) {
       //code goes here
    }
    
    $van_besch='18:00';
    $tot_besch='22:00';
    $van='13:00';
    $tot='19:00';
    if(strotime($van)strotime($tot_-besch)){
    //代码在这里
    }
    
    这是在数据库中还是仅在PHP中?实际上,值将来自数据库。但我用已知变量替换了它们。因此,这是PHP。首先,您是否确保您在代码中使用的日期时间格式与您在数据库中使用的格式相同?如果已经相同,请告知我们格式?您需要您可以构造查询来检查员工的工作时间,如果有重叠,则不要插入。那么,跨越一天休息的班次呢?
    22:00-02:00
    ?您的代码也需要处理此类情况。您实际上可以将这两种逻辑组合到一个语句中。
    $van=$van_besch
    。但您也需要这样做请确保在所有情况下,
    $van@squegy这正是我需要的答案。这非常有效!例如,它只适用于17.00-02.00。需要找出解决方法。That@StanvanderAvoird看看我下面的回答。我讨论了这个问题,并把它分成两个比较,在午夜分,或者用一种非常便宜的方法这将是在结束时间的基础上增加24小时,使其成为
    17:00-26:00
    ,只要确保在午夜过后的其他时间也这样做即可。@squegy似乎是合法的。但我不理解你的方法。我用你的表和你的查询尝试过它,但无论如何它都会返回我1。@Stan van der Avoird做了我编辑过的解决方案为你锻炼?实际上最好使用
    DateTime
    ,因为它考虑了时区和DST。是的,像这样的
    $exampleTime=newdatetime($van)
    会起作用,然后比较它们。
    $van_besch = '18:00';
    $tot_besch = '22:00';
    
    $van = '13:00';
    $tot = '19:00';
    
    if (strtotime($van) < strtotime($van_besch) || strtotime($tot ) > strtotime($tot_besch)) {
       //code goes here
    }