kylekatarnls / business-time

Carbon mixin to handle business days and opening hours
MIT License
297 stars 14 forks source link

Getting {} with isOpenOn() #53

Closed JeroenSteen closed 2 years ago

JeroenSteen commented 2 years ago

I have a JSON file with my working hours, and I'm trying to check if I'm open on a non-open day like for example saturday. But I'm getting as result only {}, so not for example false. When I check monday a open-day day I also get {}, and then it should be true. This is my data:

{
    "working": {
        "monday": ["11:00-21:00"],
        "tuesday": ["16:00-21:00"],
        "wednesday": [],
        "thursday": ["11:00-21:00"],
        "friday": ["16:00-21:00"],
        "saturday": [],
        "sunday": ["12:00-16:00"]
    }
}

And this is my PHP file:

use Carbon\Carbon;
use Cmixin\BusinessTime;

$availabilites = json_decode(file_get_contents("/config/availability_hours.json"), true);
$workingDays = BusinessTime::enable(Carbon::class, $availabilites["working"]);
echo $workingDays->isOpenOn("saturday"); // Only {} as result.
echo $workingDays->isOpenOn("monday"); // Only {} as result.

I by the way want to use multiple 'openinghours', for working, deadlines and pickup times.

kylekatarnls commented 2 years ago

Hello,

In your example $workingDays is the instance of the mixin. It's not meant to be used this way.

Please follow the examples of the documentation:

BusinessTime::enable(Carbon::class, $availabilites["working"]);

var_dump(Carbon::isOpenOn("saturday"));
var_dump(Carbon::isOpenOn("monday")); 

// Or On precise moment:

var_dump(Carbon::parse('2022-04-16 10:00')->isOpen()); 
var_dump(Carbon::parse('2022-04-16 12:00')->isOpen());