susanBuck / e15-spring22

0 stars 0 forks source link

Laravel - How to perfrm GT or LT functions with Time #39

Closed mgarimelHES closed 2 years ago

mgarimelHES commented 2 years ago

Hi,

I am wondering is there an operator or something I could use to perform the following -

current time < 10 pm
current time > 5 am

I have tried something like ' currentTime => 'date_format:H:i|after: 5 am'

But no luck, is it something possible with Laravel Validate?

Thanks

susanBuck commented 2 years ago

Hi @mgarimelHES -

Something like this?

# Get the current hour in 24 hour format
$now = date('G');

# Times we're comparing to
$startHour = '5am';
$endHour = '10pm';

# Convert start/end times to 24 hour format; REF:
# https://www.php.net/manual/en/function.date.php
# https://www.php.net/manual/en/function.strtotime.php
$startHour = date("G", strtotime($startHour));
$endHour = date("G", strtotime($endHour));

# Do the comparison
if ($now > $startHour and $now < $endHour) {
    dump('Within time range');
} else {
    dump('Outside time range');
}

In terms of using this as part of Laravel’s Validation features, you'd have to set it up using custom validation feature: https://laravel.com/docs/9.x/validation#custom-validation-rules

If you go this path, I suggest using the Closure option, as it's the most straightforward: https://laravel.com/docs/9.x/validation#using-closures

If you dig into custom validation and have questions, let me know.

mgarimelHES commented 2 years ago

Hi Susan,  Thanks, but I have issues using the Closure option. For example, the time (hour) is not coming in 24 hr format, secondly if the start time is 8 am and end time is 1 pm, the logic will fail since it is not converting the hour to 24 hour format. I don't want to use Java script as a part of client level validation since it is not recommended.

susanBuck commented 2 years ago

Hi @mgarimelHES -

Re: the time not coming in as 24 hour - the code sample above accommodates for that, converting the time to 24 hour format.

I'm going to take a look at your code base now to get a clearer picture of what you're working with. I'll reply again shortly.

susanBuck commented 2 years ago

Hi @mgarimelHES - What about something like this:

$fromTime = $request->input('fromTime', null);
$toTime = $request->input('toTime', null);

$request->validate([
    'parkingDay' => 'required',
    'discountType' => 'required',
    'plate' => 'required',
    'make' => 'required',
    'model' => 'required',
    'terms' => 'required',
    'fromTime' => ['required',
    function ($attribute, $value, $fail) use ($toTime) {
        # Get the hour in 24 hour format
        $fromTime = date("G", strtotime($value));
        $toTime = date("G", strtotime($toTime));

        if ($fromTime < 6 or $fromTime >= 22) {
            $fail('Parking start time has to be between 6am and 10pm');
        }

        if ($fromTime > $toTime) {
            $fail('Parking start time can not be later than end time');
        }
    }],
    'toTime' => ['required',
    function ($attribute, $value, $fail) use ($fromTime) {
        # Get the hour in 24 hour format
        $toTime = date("G", strtotime($value));

        if ($toTime < 6 or $toTime >= 22) {
            $fail('Parking end time has to be between 6am and 10pm');
        }
    }]
]);

This validates that the start and end time fall between 6am and 10pm. It also validates that the end time is not earlier than the start time. Let me know if you had something else in mind.

mgarimelHES commented 2 years ago

It is resolved, I have made it simple and it looks good for now.