未加星标

Managing opening hours with PHP

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

For several different clients we needed to display a schedule of opening hours on their websites. They also wanted to display if a department / store / … is open on the moment you visit the site. My colleague Seb extracted all the functionality around opening hours to the newly released opening-hours package .

You create an OpeningHours object that describes a business’ opening hours. It can be queried for open or closed on days or specific dates, or use to present the times per day.

A set of opening hours is created by passing in a regular schedule, and a list of exceptions.

$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' => [], ], ]);

The object can be queried for a day in the week, which will return a result based on the regular schedule:

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

It can also be queried for a specific date and time:

// 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

It can also return arrays of opening hours for a week or a day:

// 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();

Take a look at the package on GitHub to learn the full api. Check out the list ofphp packages we’ve previously made to see if we’ve made something else that could be of use to you.

Freek Van der Herten

Freek Van der Herten is a partner and developer at Spatie , an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band .

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: PHPLaravelGitGitHub
分页:12
转载请注明
本文标题:Managing opening hours with PHP
本站链接:http://www.codesec.net/view/481739.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(30)