对于几个不同的客户,我们需要在他们的网站上显示开放时间的时间表。他们还想在您访问该网站的那一刻显示百货公司/商店/...是否开放。我的同事Seb将开放时间周围的所有功能提取到新发布的opening-hours包中

您创建了一个OpeningHours描述企业营业时间的对象。它可以查询openclosed日期或特定日期,或用于显示每天的时间。

通过传入常规时间表和例外列表来创建一组开放时间。

$openingHours = OpeningHours::create([
    'monday' => ['09:00-12:00', '13:00-18:00'],
    'tuesday' => ['09:00-12:00', '13:00-18:00'],
    'wednesday' => ['09:00-12:00'],
    'thursday' => ['09:00-12:00', '13:00-18:00'],
    'friday' => ['09:00-12:00', '13:00-20:00'],
    'saturday' => ['09:00-12:00', '13:00-16:00'],
    'sunday' => [],
    'exceptions' => [
        '2016-11-11' => ['09:00-12:00'],
        '2016-12-25' => [],
    ],
]);

该对象可以查询一周中的一天,它会根据常规时间表返回结果:

// Open on Mondays:
$openingHours->isOpenOn('monday'); // true

// Closed on Sundays:
$openingHours->isOpenOn('sunday'); // false

也可以查询特定的日期和时间:

// Closed because it's after hours:
$openingHours->isOpenAt(new DateTime('2016-09-26 19:00:00')); // false

// Closed because Christmas was set as an exception
$openingHours->isOpenAt(new DateTime('2016-12-25')); // false

它还可以返回一周或一天的开放时间数组:

// OpeningHoursForDay object for the regular schedule
$openingHours->forDay('monday');

// OpeningHoursForDay[] for the regular schedule, keyed by day name
$openingHours->forWeek();

// OpeningHoursForDay object for a specific day
$openingHours->forDate(new DateTime('2016-12-25'));

// OpeningHoursForDay[] of all exceptions, keyed by date
$openingHours->exceptions();

查看GitHub的包以了解完整的 api。查看我们之前制作的 PHP 包列表,看看我们是否制作了其他可能对您有用的东西。


qmister
88 声望1 粉丝