本文介绍了从开始和结束时间在PHP中分配20分钟的时间段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

Ex->

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

根据我的示例-开始时间为上午9点到晚上7-45pm

so as per my example - start time is 9 am to 7-45pm

所以我希望有20分钟的时段。在PHP中,例如

so i want possible slot of 20 min. in php like

9:00-9:20,9-20至9-40等。而且还预订了 ex 12:00-12:20,2:40至2:60 ,则该价格将不包含在此列表中。

9:00 - 9:20 , 9-20 to 9-40 etc. but also for ex 12:00-12:20 , 2:40 to 2:60 booked then it will not included in this list.

我想腾出20分钟的时间。是的,但是它还会过滤alr

I want to make 20 min slot. yes but it will also filter alr

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

Using the time difference in php i have start time and end time.

然后我要检查从开始时间到结束时间可能有20分钟的时间段

And then i want to check that how many 20 min slot possible from start and end time

=>

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


推荐答案

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

This will do what you need. But specific format should be kept for array elements, for easy calculations.

<?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";

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

If you use booked datetime as array, use below code.

<?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";

这篇关于从开始和结束时间在PHP中分配20分钟的时间段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 22:29