Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. DateTimeNormalizerContextBuilder.php

function DateTimeNormalizerContextBuilder::withTimezone

Configures the timezone of the date.

It could be either a \DateTimeZone or a string that will be used to construct the \DateTimeZone

Throws

InvalidArgumentException

See also

https://secure.php.net/manual/en/class.datetimezone.php

File

vendor/symfony/serializer/Context/Normalizer/DateTimeNormalizerContextBuilder.php, line 48

Class

DateTimeNormalizerContextBuilder
A helper providing autocompletion for available DateTimeNormalizer options.

Namespace

Symfony\Component\Serializer\Context\Normalizer

Code

public function withTimezone(\DateTimeZone|string|null $timezone) : static {
    if (null === $timezone) {
        return $this->with(DateTimeNormalizer::TIMEZONE_KEY, null);
    }
    if (\is_string($timezone)) {
        try {
            $timezone = new \DateTimeZone($timezone);
        } catch (\Exception $e) {
            throw new InvalidArgumentException(\sprintf('The "%s" timezone is invalid.', $timezone), previous: $e);
        }
    }
    return $this->with(DateTimeNormalizer::TIMEZONE_KEY, $timezone);
}
RSS feed
Powered by Drupal