Tribe__Date_Utils::immutable( string|DateTime|int $datetime = 'now', string|DateTimeZone|null $timezone = null, bool $with_fallback = true )

Builds the immutable version of a date from a string, integer (timestamp) or \DateTime object.

It’s the immutable version of the Tribe__Date_Utils::build_date_object method.


Parameters

$datetime

(string|DateTime|int) (Optional) A strtotime parse-able string, a DateTime object or a timestamp; defaults to now.

Default value: 'now'

$timezone

(string|DateTimeZone|null) (Optional) A timezone string, UTC offset or DateTimeZone object; defaults to the site timezone; this parameter is ignored if the $datetime parameter is a DatTime object.

Default value: null

$with_fallback

(bool) (Optional) Whether to return a DateTime object even when the date data is invalid or not; defaults to true.

Default value: true


Top ↑

Return

(DateTimeImmutable|false) A DateTime object built using the specified date, time and timezone; if $with_fallback is set to false then false will be returned if a DateTime object could not be built.


Top ↑

Source

File: src/Tribe/Date_Utils.php

		static function immutable( $datetime = 'now', $timezone = null, $with_fallback = true ) {
			if ( $datetime instanceof DateTimeImmutable ) {
				return $datetime;
			}

			if ( $datetime instanceof DateTime ) {
				return DateTimeImmutable::createFromMutable( $datetime );
			}

			$mutable = static::build_date_object( $datetime, $timezone, $with_fallback );

			if ( false === $mutable ) {
				return false;
			}

			$cache_key = md5( ( __METHOD__ . $mutable->getTimestamp() ) );
			$cache     = tribe( 'cache' );

			if ( false !== $cached = $cache[ $cache_key ] ) {
				return $cached;
			}

			$immutable = DateTimeImmutable::createFromMutable( $mutable );

			$cache[ $cache_key ] = $immutable;

			return $immutable;
		}

Top ↑

Changelog

Changelog
Version Description
4.10.2 Introduced.