return function () {
$pickers = ['eonasdan', 'native', 'flatpickr', 'tempus'];
$picker = in_array($_GET['picker'] ?? '', $pickers, true) ? $_GET['picker'] : 'flatpickr';
$lang = in_array($_GET['lang'] ?? '', ['ru', 'uk', 'de'], true) ? $_GET['lang'] : '';
$lang !== '' && conf('language', $lang);
$query = '?picker=' . $picker . ($lang !== '' ? '&lang=' . $lang : '');
$self = url('/@object/@action/' . urlencode('form/datetime_pickers'));
// A second form that htmx replaces: the starter destroys the old picker before the swap and starts the new one after it
$note_form = function ($n) use ($self, $query, $picker) {
$n = max(1, min(15, (int) $n));
return '<div id="dp_htmx">' . form(['dp_note' => '2026-11-' . (10 + $n)], ['id' => 'dp_note_form', '__form_id__' => 'docs_dp_note'])
->datetime_select('dp_note', 'Note date', ['picker' => $picker])
. '<button type="button" class="btn btn-default btn-outline-secondary" id="dp_htmx_reload" hx-get="' . $self . $query . '¬e=' . ($n + 1)
. '" hx-target="#dp_htmx" hx-swap="outerHTML" hx-push-url="true">Replace with htmx (note ' . ($n + 1) . ')</button></div>';
};
$htmx = _class('htmx');
header('Vary: HX-Request, HX-History-Restore-Request');
if ($htmx->_wants_fragment() && isset($_GET['note'])) {
return $htmx->_result($note_form($_GET['note']));
}
asset('htmx');
$links = [];
foreach ($pickers as $p) {
$links[] = $p === $picker ? '<b>' . $p . '</b>' : '<a href="' . $self . '?picker=' . $p . '">' . $p . '</a>';
}
$intro = '<p class="docs-demo-intro">The <code>datetime_select()</code> control with each picker of
<code>yf_form2_datetime::$PICKER</code> (or the field option <code>picker</code>): ' . implode(', ', $links) . '.
eonasdan (the default) needs jQuery and moment; native uses the browser date inputs and posts ISO dates (2026-11-15,
2026-11-15T14:30); flatpickr and Tempus Dominus need no jQuery and post 15.11.2026 [14:30] as eonasdan does. The fields start
with stored values (a d.m.Y text, a DB datetime, a DB date, a DB time), the third one allows 5..25 Nov 2026 without 11 Nov
(the native picker cannot disable single dates, so it only gets the range). Below, the server shows what it received and how
strtotime(), the valid_date rule and the table2 filter conditions read it. The last form is replaced by htmx. The demo stores
nothing. Classes: <code>yf_form2</code>, <code>yf_form2_datetime</code>.</p>';
$form_id = 'docs_datetime_pickers';
$stored = ['dp_date' => '15.11.2026', 'dp_datetime' => '2026-11-15 14:30:00', 'dp_minmax' => '2026-11-12', 'dp_time' => '09:45:00'];
$result = '';
$form = form((array) $_POST + $stored, ['csrf' => true, '__form_id__' => $form_id, 'id' => 'dp_form', 'action' => $self . $query])
->validate([
'__form_id__' => $form_id,
'dp_date' => 'trim|required|valid_date',
'dp_datetime' => 'trim|required|valid_date',
'dp_minmax' => 'trim|required|valid_date',
'dp_time' => 'trim|required',
])
->on_validate_ok(function ($data) use (&$result) {
$filter = _class('table2_filter', 'classes/table2/');
$filter->_init();
$conds = $filter->supported_conds;
$validate = _class('validate');
$out = [];
foreach (['dp_date', 'dp_datetime', 'dp_minmax', 'dp_time'] as $name) {
$value = (string) ($data[$name] ?? '');
$time = strtotime($value);
$out[$name] = [
'raw' => $value,
'strtotime' => $time === false ? false : date('Y-m-d H:i:s', $time),
'valid_date' => $validate->valid_date($value),
];
}
// the table2 "between" filters, as if dp_date were <field> and dp_minmax <field>__and
$pair = ['value' => (string) $data['dp_date'], 'and' => (string) $data['dp_minmax']];
$out['table2'] = [
'dt_between' => $conds['dt_between']($pair),
'datetime_between' => $conds['datetime_between']($pair),
'daterange_dt_between' => $conds['daterange_dt_between'](['value' => $pair['value'] . ' - ' . $pair['and']]),
];
$json = json_encode($out, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$result = '<div class="alert alert-success" id="dp_result"><b>Received</b> (nothing stored):<pre>' . _prepare_html($json, 0) . '</pre></div>';
})
->datetime_select('dp_date', 'Date', ['picker' => $picker])
->datetime_select('dp_datetime', 'Date and time', ['picker' => $picker, 'with_time' => 1, 'stepping' => 15, 'side_by_side' => 1])
->datetime_select('dp_minmax', 'Min / max', ['picker' => $picker, 'min_date' => strtotime('2026-11-05'), 'max_date' => strtotime('2026-11-25')]
+ ($picker === 'native' ? [] : ['disabled_dates' => ['2026-11-11']]))
->datetime_select('dp_time', 'Time', ['picker' => $picker, 'no_date' => 1, 'no_time' => 0, 'stepping' => 5])
->save();
// A failed CSRF check logs into the log_csrf_errors table; the demo writes no rows
$guard = _class('csrf_guard');
$log_errors = $guard->LOG_ERRORS;
$guard->LOG_ERRORS = false;
try {
$html = (string) $form;
} finally {
$guard->LOG_ERRORS = $log_errors;
}
return $intro . $result . $html . '<h4 id="dp_htmx_title">Replaced by htmx</h4>' . $note_form(1);
};