PHP使用DatePeriod创建具有中断时间的时隙 [英] PHP Create Timeslots with break timing using DatePeriod

查看:184
本文介绍了PHP使用DatePeriod创建具有中断时间的时隙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用开始,结束时间&也可以打破时机。

I want to create time slots with start,end time & also break timing.

public function getServiceScheduleSlots($duration,$break, $stTime,$enTime)
{
    $start = new DateTime($stTime);
    $end = new DateTime($enTime);
    $interval = new DateInterval("PT" . $duration. "M");
    $period = new DatePeriod($start, $duration, $end);

    foreach ($period as $dt) {
        $periods[] = $dt->format('H:iA');
    }
    return $periods;
}

例如,

我的服务开始时间 10:00 AM ,结束时间 12:00 PM

条件:每个服务时间 30分钟& 15分钟中断。

For ex.,
My service start time 10:00 AM , End Time 12:00 PM.
Condition: each service time 30 min & 15 min break.

以上方法返回如下,


  • 10:00 AM - 10:30 AM

  • 上午10:30 - 11:00 AM

  • 11:00 AM - 11:30 AM

  • 11:30 AM - 12:00 PM

  • 10:00 AM - 10:30 AM
  • 10:30 AM - 11:00 AM
  • 11:00 AM - 11:30 AM
  • 11:30 AM - 12:00 PM

预期结果as,


  • 10:00 AM - 10:30 AM

  • 10:45 AM - 11 :15 AM

  • 11:30 AM - 12:00 PM

我要添加

提前感谢

推荐答案

如何....

function getServiceScheduleSlots($duration,$break, $stTime,$enTime)
{
        $start = new DateTime($stTime);
        $end = new DateTime($enTime);
        $interval = new DateInterval("PT" . $duration. "M");
        $breakInterval = new DateInterval("PT" . $break. "M");

        for ($intStart = $start; 
             $intStart < $end; 
             $intStart->add($interval)->add($breakInterval)) {

               $endPeriod = clone $intStart;
               $endPeriod->add($interval);
               if ($endPeriod > $end) {
                 $endPeriod=$end;
               }
               $periods[] = $intStart->format('H:iA') . 
                            ' - ' . 
                            $endPeriod->format('H:iA');
        }

        return $periods;
  }

这篇关于PHP使用DatePeriod创建具有中断时间的时隙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆