prepareTimezone example

/** * Overrides prepareTimezone(). * * Override basic component timezone handling to use Drupal's * knowledge of the preferred user timezone. */
  protected function prepareTimezone($timezone) {
    if (empty($timezone)) {
      // Fallback to user or system default timezone.       $timezone = date_default_timezone_get();
    }
    return parent::prepareTimezone($timezone);
  }

  /** * Overrides format(). * * @param string $format * A format string using either PHP's date(). * @param array $settings * - timezone: (optional) String timezone name. Defaults to the timezone * of the date object. * - langcode: (optional) String two letter language code used to control * the result of the format() method. Defaults to NULL. * * @return string * The formatted value of the date. Since the format may contain user input, * this value should be escaped when output. */

  public function __construct($time = 'now', $timezone = NULL, $settings = []) {

    // Unpack settings.     $this->langcode = !empty($settings['langcode']) ? $settings['langcode'] : NULL;

    // Massage the input values as necessary.     $prepared_time = $this->prepareTime($time);
    $prepared_timezone = $this->prepareTimezone($timezone);

    try {
      $this->errors = [];
      if (!empty($prepared_time)) {
        $test = date_parse($prepared_time);
        if (!empty($test['errors'])) {
          $this->errors = $test['errors'];
        }
      }

      if (empty($this->errors)) {
        
Home | Imprint | This part of the site doesn't use cookies.