$sanitizer->date() method

Sanitize a date or date/time string, making sure it is valid, and return it

  • If no date $format is specified, date will be returned as a unix timestamp.
  • If given date in invalid format and can’t be made valid, or date is empty, NULL will be returned.
  • If $value is an integer or string of all numbers, it is always assumed to be a unix timestamp.
  • If $format and “strict” option specified, date will also validate for format and no out-of-bounds values will be converted.

Usage

// basic usage
$string = $sanitizer->date($value);

// usage with all arguments
$string = $sanitizer->date($value, $format = null, array $options = []);

Arguments

NameType(s)Description
valuestring, int

Date string or unix timestamp

format (optional)string, null

Format of date string ($value) in any wireDate(), date() or strftime() format.

options (optional)array

Options to modify behavior:

  • returnFormat (string): wireDate() format to return date in. If not specified, then the $format argument is used.
  • min (string|int): Minimum allowed date in $format or unix timestamp format. Null is returned when date is less than this.
  • max (string|int): Maximum allowed date in $format or unix timestamp format. Null is returned when date is more than this.
  • default (mixed): Default value to return if no value specified.
  • strict (bool): Force dates that don’t match given $format, or out of bounds, to fail. Requires $format. (default=false)

Return value

string int null


$sanitizer methods and properties

API reference based on ProcessWire core version 3.0.236

Latest news

  • ProcessWire Weekly #551
    In the 551st issue of ProcessWire Weekly we'll check out what's new in the core this week, share a new weekly poll, and more. Read on!
    Weekly.pw / 1 December 2024
  • Custom Fields Module
    This week we look at a new ProFields module named Custom Fields. This module provides a way to rapidly build out ProcessWire fields that contain any number of subfields/properties within them.
    Blog / 30 August 2024
  • Subscribe to weekly ProcessWire news

“Indeed, if ProcessWire can be considered as a CMS in its own right, it also offers all the advantages of a CMF (Content Management Framework). Unlike other solutions, the programmer is not forced to follow the proposed model and can integrate his/her ways of doing things.” —Guy Verville, Spiria Digital Inc.