Oihana PHP

ISO8601DurationRule extends Rule

Validates whether a value is a valid ISO 8601 duration string.

The ISO 8601 duration format follows the pattern : P[n]Y[n]M[n]W[n]DT[n]H[n]M[n]S

✅ Supported examples:

P1Y2M3D       // 1 year, 2 months, 3 days
PT4H30M       // 4 hours, 30 minutes
P1W           // 1 week
P0D           // zero duration
P30D

❌ Invalid examples:

P             // no components
1Y2M          // missing P
P1.5Y         // decimals rejected in strict mode

Modes:

  • $strict = true (default) — regex-based validation, mandates at least one component and rejects decimals.
  • $strict = false — delegates to PHP's DateInterval parser, which is more permissive.

Empty / null values pass — the rule is "shape" only, declare Rules::REQUIRED separately when the field is mandatory.

Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Constants

NAME  : string = \oihana\validations\enums\Rules::ISO8601_DURATION
The rule name, as registered in the validation factory.

Properties

$message  : string
The message pattern used when the rule fails.
$strict  : bool
Whether the rule runs in strict (regex) mode.

Methods

__construct()  : mixed
Creates a new ISO8601DurationRule instance.
check()  : bool
Checks whether the given value satisfies the rule.

Constants

Properties

$message

The message pattern used when the rule fails.

protected string $message = "The :attribute is not a valid ISO 8601 duration expression."

Methods

__construct()

Creates a new ISO8601DurationRule instance.

public __construct([bool $strict = true ][, string|null $message = null ]) : mixed
Parameters
$strict : bool = true

When true (default), uses regex validation instead of DateInterval.

$message : string|null = null

Optional custom error message.

check()

Checks whether the given value satisfies the rule.

public check(mixed $value) : bool
Parameters
$value : mixed

The value to validate.

Return values
bool

True if the value is a valid ISO 8601 duration; false otherwise.

On this page

Search results