I have a model in which we can choose the opening hours of the institution for each day of the week, from such and such to such and such, for example Monday 12:00 AM - 11:30 PM
<?php
namespace common\models;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\Expression;
class WorkHours extends _source_WorkHours
{
public const WEEK_DAY_MON = 'Mon';
public const WEEK_DAY_TUE = 'Tue';
public const WEEK_DAY_WED = 'Wed';
public const WEEK_DAY_THU = 'Thu';
public const WEEK_DAY_FRI = 'Fri';
public const WEEK_DAY_SAT = 'Sat';
public const WEEK_DAY_SUN = 'Sun';
/**
* {@inheritdoc}
*/
public function behaviors(): array
{
return [
'timestamp' => [
'class' => TimestampBehavior::class,
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
],
'value' => new Expression('NOW()'),
],
];
}
public static function weekDays(): array
{
return [
self::WEEK_DAY_MON => 'Monday',
self::WEEK_DAY_TUE => 'Tuesday',
self::WEEK_DAY_WED => 'Wednesday',
self::WEEK_DAY_THU => 'Thursday',
self::WEEK_DAY_FRI => 'Friday',
self::WEEK_DAY_SAT => 'Saturday',
self::WEEK_DAY_SUN => 'Sunday',
];
}
public static function getWeekDay(string $val): string
{
$ar = self::weekDays();
return $ar[$val] ?? $val;
}
public static function hoursList(): array
{
$list = [];
for ($i = 0; $i < 24; $i++) {
$A = 'AM';
$n = $i;
if ($i >= 12) {
$A = 'PM';
$n = $i - 12;
}
$n = $n < 10 ? '0' . $n : $n;
$_A = ($i === 12 ? 'AM' : $A);
$list[$i . '.00'] = $n . '.00 ' . ($i === 0 ? 'PM' : $_A);
$list[$i . '.30'] = $n . '.30 ' . $A;
}
return $list;
}
}
Next, I use this model when creating an institution, and the ability to choose the opening hours there
<div class="form-group">
<label class="control-label col-sm-3">Wokr Hours</label>
<div class="col-sm-6">
<div class="form-group">
<?php foreach (WorkHours::weekDays() as $weekDay => $day) { ?>
<div class="col-sm-2"><?= $weekDay ?></div>
<div class="col-sm-5">
<?= $form->field(
$restaurantWorkHoursForm,
RestaurantWorkHoursForm::getAttributeName($weekDay, 'open'),
['template' => '<div class="col-sm-12">{input}</div>']
)->dropDownList(WorkHours::hoursList(), ['prompt' => ''])->label(false) ?>
</div>
<div class="col-sm-5">
<?= $form->field(
$restaurantWorkHoursForm,
RestaurantWorkHoursForm::getAttributeName($weekDay, 'close'),
['template' => '<div class="col-sm-12">{input}</div>']
)->dropDownList(WorkHours::hoursList(), ['prompt' => ''])->label(false) ?></div>
<?php } ?>
</div>
</div>
</div>
I also use this model when booking a place in an institution where you can choose the time, but here you can choose any time of the day
<a class="btn btn-fourth <?= $restaurantReservationForm->getErrors('time') ? 'btn-error' : '' ?>"
id="reservation-time" role="button" data-bs-toggle="dropdown" aria-expanded="false">
<span class="icon br-clock"></span> <span class="js-value">
<?= $restaurantReservationForm->time
? WorkHours::hoursList()[$restaurantReservationForm->time] : '-- : --' ?>
</span>
</a>
<ul class="dropdown-menu dropdown-menu-height-fixed" aria-labelledby="reservation-time">
<?php foreach (WorkHours::hoursList() as $k => $v) { ?>
<li><a class="dropdown-item <?= $restaurantReservationForm->time === $k ? 'active' : ''
?>" href="#" data-value="<?= $k ?>"><?= $v ?></a></li>
<?php } ?>
</ul>
Now what is the question. I have two forms. In one, the time was indicated, in the other there should be a choice of time from the first form. How can you link the opening hours of the establishment that we indicated with the choice of the time of booking?
Let's say we have a working time 12:00 AM - 11:30 PM, any time is available in the booking, but it is necessary that there is a choice only within the opening hours of the institution that we specified earlier, that is, from 12:00 AM to 11:30 PM, and so on every day, if the time is not selected in the institution for some day , then the booking will also be empty
Is there a way to do this via json ?