wpseek.com
Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



rest_parse_date › WordPress Function

Seit4.4.0
Veraltetn/v
rest_parse_date ( $date, $force_utc = false )
Parameter: (2)
  • (string) $date RFC3339 timestamp.
    Erforderlich: Ja
  • (bool) $force_utc Optional. Whether to force UTC timezone instead of using the timestamp's timezone. Default false.
    Erforderlich: Nein
    Standard: false
Gibt zurück:
  • (int|false) Unix timestamp on success, false on failure.
Definiert in:
Codex:

Parses an RFC3339 time into a Unix timestamp.

Explicitly check for false to detect failure, as zero is a valid return value on success.


Quellcode

function rest_parse_date( $date, $force_utc = false ) {
	if ( $force_utc ) {
		$date = preg_replace( '/[+-]\d+:?\d+$/', '+00:00', $date );
	}

	$regex = '#^\d{4}-\d{2}-\d{2}[Tt ]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}(?::\d{2})?)?$#';

	if ( ! preg_match( $regex, $date, $matches ) ) {
		return false;
	}

	return strtotime( $date );
}