waDateValidator

Validation of dates.

Contents...

Methods

  • isValid

    Validates a date value.

  • isEmpty

    Checks whether a specified date value is empty.

public function isValid ($value)

Validates a date value.

Parameters

  • $value

    Date value in one of the following formats:

    • string matching pattern “yyyy-mm-dd”; e.g., “2000-12-31”.
    • array with the following keys:
      • 'year': positive integer greater than 0;
      • 'month': positive integer from 1 to 12;
      • 'day': positive integer greater than 0 and not greater than the number of days in the specified 'month' key; or simply greater than 0 if no month is specified.

Example

$validator = new waDateValidator();

// false if today is December 30, 2000 because specified date is compared with the current date
wa_dumpc($validator->isValid('2000-12-31'));

// true because "February 31" formally looks correctly and such a date is automatically converted to "March 2"
wa_dumpc($validator->isValid('2000-02-31'));

// false because no month can contain more than 31 days
wa_dumpc($validator->isValid('2000-02-32'));

// true
wa_dumpc($validator->isValid([
    'year' => 2000,
    'month' => 2,
    'day' => 29,
], false));

public function isEmpty ($value)

Checks whether a specified date value is empty. Returns true in the following cases:

  • $value contains an empty string or null;
  • $value contains an array in which none of keys 'year', 'month' or 'day' contains a non-empty value.

Parameters

  • $value

    Date value in any format.

Example

$validator = new waDateValidator();

// true
wa_dumpc($validator->isEmpty(''));

// true
wa_dumpc($validator->isEmpty(null));

// true
wa_dumpc($validator->isEmpty([]));

// false
wa_dumpc($validator->isEmpty([
    'year' => 2000,
]));

// false
wa_dumpc($validator->isEmpty([
    'year' => 2000,
    'month' => 2,
    'day' => 29,
]));