Skip to main content
Version: Fegg 5.x

Validation::date

Check the format of a date

Description​

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

The separators / and - are removed, then the value must be digits only: YYYY (a year), YYYYMM (a year and a month from 01 to 12) or YYYYMMDD (an existing date, checked with checkdate()). So 2024, 2024/04, 2024-04-01 and 20240401 are valid; 2024/4/1 is not (8 digits are required for a day).

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->date('success', '2024-04-01', array('@date', 'Date')); // -> true
$validation->date('failed', '2024-02-30', array('@date', 'Date')); // -> false

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