Skip to main content
Version: Fegg 5.x

Validation::datetime

Check a date and time, such as the value of <input type="datetime-local">

Description​

public boolean Validation::datetime( string $name, mixed $value, [ mixed $code = '' ] )

Valid when the value is an existing date and time in one of these forms:

  • 2026-11-10T10:00 (as <input type="datetime-local"> sends it) or 2026-11-10 10:00
  • with seconds: 2026-11-10 10:00:30
  • a date alone: 2026-11-10 (read as midnight)

/ can be used instead of - (2026/11/10 10:00). A date that does not exist (2026-02-30) or a time such as 25:00 is invalid. An empty value is valid: use required for a required field.

Save the value in the database format with Validation::toDatetime():

public static string|null Validation::toDatetime( mixed $value )

It returns the date and time as Y-m-d H:i:s (2026-11-10 10:00:00), or null when datetime() would refuse the value.

Arguments​

NameData typeDefaultRemarks
$namestringField name (key of the error message)
$valuemixedValue to check
$codemixed''Error code, array of error code and parameters, or error message (see Configuration)

Example​

Controller
use Fegg\Tool\Validation;

$validation = new Validation();

$validation->datetime('success', '2026-11-10T10:00', array('@date', 'Date')); // -> true
$validation->datetime('failed', '2026-02-30 10:00', array('@date', 'Date')); // -> false

var_dump($validation->getErrorMessage());
// -> array('failed' => '<strong>Date</strong> の日付では登録できません')

Validation::toDatetime('2026-11-10T10:00'); // -> '2026-11-10 10:00:00'
Validation::toDatetime('2026-02-30'); // -> null