Comments (5)
Hello, you can build your $exceptions
list with PHP native DatePeriod
:
$exceptions = [];
foreach (new DatePeriod(new DateTimeImmutable('2016-11-11'), new DateInterval('P1D'), new DateTimeImmutable('2016-11-25')) as $day) {
$exceptions[$day->format('Y-m-d')] = ['09:00-12:00'];
}
//...
'exceptions' => $exceptions,
from opening-hours.
Hey @kylekatarnls of course I can and this is what I would do as workaround but the point is I want to save such opening hours in database and it is unnecessary extra data for the related ranges therefore I asked.
So saving e.g.: '2016-11-11-2016-11-25' => ['09:00-12:00'], would be much less as:
'2016-11-11' =>
['09:00-12:00'],
'2016-11-12' =>
['09:00-12:00'],
'2016-11-13' =>
['09:00-12:00'],
'2016-11-14' =>
['09:00-12:00'],
'2016-11-15' =>
['09:00-12:00'],
'2016-11-16' =>
['09:00-12:00'],
'2016-11-17' =>
['09:00-12:00'],
'2016-11-18' =>
['09:00-12:00'],
'2016-11-19' =>
['09:00-12:00'],
'2016-11-20' =>
['09:00-12:00'],
'2016-11-21' =>
['09:00-12:00'],
'2016-11-22' =>
['09:00-12:00'],
'2016-11-23' =>
['09:00-12:00'],
'2016-11-24' =>
['09:00-12:00'],
also this is a simple example, imagine several opening hours with a ton of exceptions - therefore you get a ton of unnecessary data to save in db.
from opening-hours.
Hello, I suggest you just add a customization step:
I also suggest to use some explicit separator, here in the example I used ' to '
so it's more readable and does not overlap -
which is already used by date format.
$openingHoursDescription = [
'monday to friday' => ['09:00-12:00', '13:00-18:00'],
'saturday to sunday'=> [],
'exceptions' => [
'2016-11-11 to 2016-11-25' => ['09:00-12:00'],
],
];
function parseOpeningHoursDescription(array $description): array
{
foreach ($description as $key => $value) {
if (is_array($value)) {
$value = parseOpeningHoursDescription($value);
}
$parts = explode(' to ', $key);
if (count($parts) === 2) {
unset($description[$key]);
$isDayName = preg_match('/^[a-z]+$/i', $parts[0]);
$start = new DateTimeImmutable($parts[0]);
$end = $start->modify($parts[1])->modify('+12 hours');
// If using PHP 8.2, you can use DatePeriod::INCLUDE_END_DATE instead of ->modify('+12 hours')
foreach (new DatePeriod($start, new DateInterval('P1D'), $end) as $day) {
$description[$day->format($isDayName ? 'l' : 'Y-m-d')] = $value;
}
continue;
}
$description[$key] = $value;
}
return $description;
}
$openingHours = OpeningHours::create(parseOpeningHoursDescription($openingHoursDescription));
Then you can save $openingHoursDescription
(before the transformation) in your database.
We might think about supporting this syntax out of the box, but need to think about the consequences of supporting multiple syntax for the same result, what if a day appears in multiple ranges and other edge cases.
from opening-hours.
Hello @kylekatarnls
Monday to Friday and stuff like that so for the common weekdays is not something what I would recommend to support for the related range feature request from my side. The reason for it is that there the related overflow is not relevant.
I just talk about the exceptions as there you often go with holidays and stuff like that which take longer than the usual opening hours.
So I end up more like something like the following as fix on my side, but anyway I see it more as a feature request where exactly this is out of the box possible, for the exceptions - so simple said exceptions supporting (date) ranges:
$openingHoursDescription = [
'monday' => ['09:00-12:00', '13:00-18:00'],
'tuesday' => ['09:00-12:00', '13:00-18:00'],
'wednesday' => ['09:00-12:00', '13:00-18:00'],
'thursday' => ['09:00-12:00', '13:00-18:00'],
'friday' => ['09:00-12:00', '13:00-18:00'],
'saturday' => [],
'sunday' => [],
'exceptions' => [
'2016-11-11 to 2016-11-25' => ['09:00-12:00'],
// Add other exception ranges as needed
],
];
function parseExceptionRanges(array $description): array
{
$exceptions = $description['exceptions'] ?? [];
unset($description['exceptions']);
foreach ($exceptions as $key => $value) {
$parts = explode(' to ', $key);
if (count($parts) === 2) {
$start = new DateTimeImmutable($parts[0]);
$end = new DateTimeImmutable($parts[1])->modify('+1 day'); // End date is exclusive
foreach (new DatePeriod($start, new DateInterval('P1D'), $end) as $day) {
$description[$day->format('Y-m-d')] = $value;
}
}
}
return $description;
}
$openingHours = OpeningHours::create(parseExceptionRanges($openingHoursDescription));
from opening-hours.
The reason for it is that there the related overflow is not relevant.
Responsibility of spatie/opening-hours
is not to ensure the passed payload can be stored below a certain size. The size of you data will depend on your case, and where you store it and how big it can be will also vary from one app to another. How to compress it is application responsibility, the library should remain agnostic.
If we implement a new feature/syntax, it has to be as generic and consistent as possible, not to solve this one specific issue but to cover various needs where ranges support can be convenient.
from opening-hours.
Related Issues (20)
- Github URL update HOT 2
- [Question/Feature Request] How to get all days of the current week including days resulting from exceptions? HOT 3
- [Bug] nextOpen calculates the wrong date for a regular day with exceptions HOT 3
- timezone is set on passed object when getting hours for a specific date HOT 2
- Free time slots during the day ? HOT 1
- nextOpen not respecting timezone HOT 2
- Timezone not working in currentOpenRange HOT 2
- I want to add schedule for specific date HOT 1
- Locale? HOT 1
- GitHub repository link not working HOT 2
- Support for holidays HOT 5
- Overflow support does not seem to work for createAndMergeOverlappingRanges HOT 2
- diffInOpenMinutes() does not work if only one day is configured as open HOT 6
- Date period in exceptions or start and end date for a set of days of the week in regular time HOT 4
- [Feature request] createFromStructuredData method
- How to destroy OpeningHours object or update it ? HOT 1
- How to create an exception using datetime with seconds HOT 2
- Adding seconds precision to exceptions HOT 2
- Output time zone taken into account in all methods? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from opening-hours.