Tribe__Timezones::apply_offset( string $datetime, int|string $offset, bool $invert = false )

Applies an time offset to the specified date time.


Parameters

$datetime

(string) (Required) The date and time string in a valid date format.

$offset

(int|string) (Required) (string or numeric offset)

$invert

(bool) (Optional) = false Whether the offset should be added (true) or subtracted (false); signum operations carry over so -(-23) = +23.

Default value: false


Top ↑

Return

(string)


Top ↑

Source

File: src/Tribe/Timezones.php

	public static function apply_offset( $datetime, $offset, $invert = false ) {
		// Normalize
		$offset = strtolower( trim( $offset ) );

		// Strip any leading "utc" text if set
		if ( 0 === strpos( $offset, 'utc' ) ) {
			$offset = substr( $offset, 3 );
		}

		// It's possible no adjustment will be needed
		if ( 0 === (int) $offset ) {
			return $datetime;
		}

		// if the offset contains fractions like :15, :30 or :45 convert them
		$supported_offsets = array(
			'/:15$/' => '.25',
			'/:30$/' => '.5',
			'/:45$/' => '.75',
		);
		$offset = preg_replace( array_keys( $supported_offsets ), array_values( $supported_offsets ), $offset );

		// Convert the offset to minutes for easier handling of fractional offsets
		$offset = (int) ( $offset * 60 );

		// Invert the offset? Useful for stripping an offset that has already been applied
		if ( $invert ) {
			$offset *= - 1;
		}

		if ( $offset > 0 ) {
			$offset = '+' . $offset;
		}

		$offset = $offset . ' minutes';

		$offset_datetime = date_create( $datetime );

		if ( $offset_datetime && $offset_datetime->modify( $offset ) ) {
			return $offset_datetime->format( Tribe__Date_Utils::DBDATETIMEFORMAT );
		}

		return $datetime;
	}