Back to Validation

Time

docs/validators/Time.md

3.1.13.2 KB
Original Source
<!-- SPDX-License-Identifier: MIT SPDX-FileCopyrightText: (c) Respect Project Contributors SPDX-FileContributor: Alexandre Gomes Gaigalas <[email protected]> SPDX-FileContributor: Henrique Moody <[email protected]> -->

Time

  • Time()
  • Time(string $format)

Validates whether an input is a time or not. The $format argument should be in accordance to PHP's date() function, but only those are allowed:

FormatDescriptionValues
g12-hour format of an hour without leading zeros1 through 12
G24-hour format of an hour without leading zeros0 through 23
h12-hour format of an hour with leading zeros01 through 12
H24-hour format of an hour with leading zeros00 through 23
iMinutes with leading zeros00 to 59
sSeconds, with leading zeros00 through 59
uMicroseconds000000 through 999999
vMilliseconds000 through 999
aLowercase Ante meridiem and Post meridiemam or pm
AUppercase Ante meridiem and Post meridiemAM or PM

When a $format is not given its default value is H:i:s.

php
v::time()->assert('00:00:00');
// Validation passes successfully

v::time()->assert('23:20:59');
// Validation passes successfully

v::time('H:i')->assert('23:59');
// Validation passes successfully

v::time('g:i A')->assert('8:13 AM');
// Validation passes successfully

v::time('His')->assert(232059);
// Validation passes successfully

v::time()->assert('24:00:00');
// → "24:00:00" must be a time in the "23:59:59" format

v::time()->assert(new DateTime());
// → `DateTime { 2024-01-01T12:00:00+00:00 }` must be a time in the "23:59:59" format

v::time()->assert(new DateTimeImmutable());
// → `DateTimeImmutable { 2024-01-01T12:00:00+00:00 }` must be a time in the "23:59:59" format

Templates

Time::TEMPLATE_STANDARD

ModeTemplate
default{{subject}} must be a time in the {{sample}} format
inverted{{subject}} must not be a time in the {{sample}} format

Template placeholders

PlaceholderDescription
subjectThe validated input or the custom validator name (if specified).
sample

Categorization

  • Date and Time

Changelog

VersionDescription
3.0.0Templates changed
2.0.0Created

See Also