从开始和结束时间在PHP的20分钟时间段

时间:2015-10-21 09:58:49

标签: javascript php codeigniter datetime time

前 - >

 Start time = 2015-10-21 09:00:00
 end time  = 2015-10-21 19:45:00

所以根据我的例子 - 开始时间是9 am to 7-45pm

所以我想要20分钟的时间段。在php喜欢

9:00 - 9:20 , 9-20 to 9-40 etc。但是对于预订的ex 12:00-12:20 , 2:40 to 2:60,它也不会包含在此列表中。

我想要20分钟。是的,但它也会过滤alr

在php中使用时差我有开始时间和结束时间。

然后我想检查从开始和结束时间开始有多少20分钟的插槽

=>它还会检查一些时间段是否已预订,然后不予考虑

IN SHOT 1 to 2 or 3-5 is break time then it will not consider. 

2 个答案:

答案 0 :(得分:3)

这将满足您的需求。但是应该为数组元素保留特定的格式,以便于计算。

<?php
$start_time = '2015-10-21 09:00:00';  //start time as string
$end_time = '2015-10-21 19:45:00';  //end time as string
$booked = array('12:20-12:40','13:00-13:20');    //booked slots as arrays
$start = DateTime::createFromFormat('Y-m-d H:i:s',$start_time);  //create date time objects
$end = DateTime::createFromFormat('Y-m-d H:i:s',$end_time);  //create date time objects
$count = 0;  //number of slots
$out = array();   //array of slots 
for($i = $start; $i<$end;)  //for loop 
{
$avoid = false;   //booked slot?
$time1 = $i->format('H:i');   //take hour and minute
$i->modify("+20 minutes");      //add 20 minutes
$time2 = $i->format('H:i');     //take hour and minute
$slot = $time1."-".$time2;      //create a format 12:40-13:00 etc
    for($k=0;$k<sizeof($booked);$k++)  //if booked hour
    {
    if($booked[$k] == $slot)  //check
    $avoid = true;   //yes. booked
    }
if(!$avoid && $i<$end)  //if not booked and less than end time
{
$count++;           //add count
$slots = ['start'=>$time1, 'stop'=>$time2];         //add count
array_push($out,$slots); //add slot to array
}
}
var_dump($out);   //array out
echo $count ." of slots available";

如果您使用预订的日期时间作为数组,请使用以下代码。

<?php
$start_time = '2015-10-21 09:00:00';  //start time as string
$end_time = '2015-10-21 19:45:00';  //end time as string
$booked = ['2015-10-21 12:20:00','2015-10-21 12:40:00', '2015-10-21 13:00:00','2015-10-21 13:20:00'];
     //booked slots as arrays
$start = DateTime::createFromFormat('Y-m-d H:i:s',$start_time);  //create date time objects
$end = DateTime::createFromFormat('Y-m-d H:i:s',$end_time);  //create date time objects
$time1 = $start;
$count = 0;  //number of slots
$out = array();   //array of slots 
for($i = $start; $i<$end;)  //for loop 
{
$avoid = false; 
$t1 = date_timestamp_get($i);
$t2 = $t1+(20*60);

    for($k=0;$k<sizeof($booked);$k+=2)  //if booked hour
    {
    $st = DateTime::createFromFormat('Y-m-d H:i:s',$booked[$k]);
    $en = DateTime::createFromFormat('Y-m-d H:i:s',$booked[$k+1]);

    if( $t1 >= date_timestamp_get($st) && $t2 <= date_timestamp_get($en)  )
    $avoid = true;   //yes. booked
    }
$slots =[ $i->format('H:i'),$i->modify("+20 minutes")->format('H:i')];
if(!$avoid && $i<$end)  //if not booked and less than end time
{
$count++;  
array_push($out,$slots);  //add slot to array
}
}
var_dump($out);   //array out
echo $count ." of slots available";

答案 1 :(得分:0)

你必须循环到最后一个单桅帆船(小于或等于end time),同时我们每次加入start time 20分钟并将其添加到数据数组

您可以在PHP文档页面上查看有关我使用的函数的更多信息:strtotimedate

<?php

    $start_time = strtotime('2015-10-21 09:00:00');
    $end_time = strtotime('2015-10-21 19:45:00');
    $slot = strtotime(date('Y-m-d H:i:s',$start_time) . ' +20 minutes');

    $data = [];

    for ($i=0; $slot <= $end_time; $i++) { 

        $data[$i] = [ 
            'start' => date('Y-m-d H:i:s', $start_time),
            'end' => date('Y-m-d H:i:s', $slot),
        ];

        $start_time = $slot;
        $slot = strtotime(date('Y-m-d H:i:s',$start_time) . ' +20 minutes');
    }

    print_r($data);

?>