GithubHelp home page GithubHelp logo

Comments (2)

kylekatarnls avatar kylekatarnls commented on June 2, 2024

Indeed, I can reproduce. I see the timezone feature was introduced for isOpenAt #16, so only for the input. At this moment the library had no nextOpen / nextClose method (no date to output).

I will investigate if we can fix it without causing breaking changes for users who might have a different usage.

Honestly I'm not keen on this timezone feature being embedded directly in spatie/opening-hours, you can get basically the same by just calling ->setTimezone() on input/output and would have a greater and less ambiguous control this way and give OpeningHours only the responsibility to check the times while ensuring the date timezones match the times of the definition:

$openingHours = \Spatie\OpeningHours\OpeningHours::create([
    'monday' => ['09:00-12:00', '13:00-18:00'],
]); // Don't pass any timezone

function nextOpen(DateTimeImmutable $date): DateTimeImmutable {
  global $openingHours;

  $timezone = $date->getTimezone();

  return $openingHours
    ->nextOpen($date->setTimezone(new DateTimeZone('America/New_York')))
    ->setTimezone($timezone); // Set back original timezone
}

$nextOpen = nextOpen(new DateTimeImmutable('2022-07-24 20:00 UTC'));

Basically you could even have a UTC input, opening-hours for a business in New York and let's say a remote user in Oslo:

$systemTimezone = 'UTC'; // Should always be UTC anyway :p
$businessSchedule = [
    'monday' => ['09:00-12:00', '13:00-18:00'],
];
$businessTimezone = 'America/New_York'; // The one to use to read the schedule above
$userTimezone = 'Europe/Oslo'; // Coming from the browser

$openingHours = \Spatie\OpeningHours\OpeningHours::create($businessSchedule); // Don't pass any timezone

$now = new \DateTimeImmutable('now', new \DateTimeZone($systemTimezone));
$nexOpen = $openingHours->nextOpen($date->setTimezone(new \DateTimeZone($businessTimezone)));

$nextOpenForUser = $nextOpen->setTimezone(new \DateTimeZone($userTimezone));

echo 'You can call us after ' . $nextOpenForUser->format('H:i') . ' (local time)';

This last case makes me think using the same timezone for input and output would not actually be generally wanted, so I wonder if the implementation of the timezone for nextOpen / nextClose method should only transform the input or also change back to original timezone the output.

from opening-hours.

kylekatarnls avatar kylekatarnls commented on June 2, 2024

Published in 2.12.0

You'll find some documentation about it on https://github.com/spatie/opening-hours#openinghourscreatearray-data-timezone--null-toutputtimezone--null-spatieopeninghoursopeninghours

from opening-hours.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.