timezone_open example

$this->assertSession()->elementTextContains('xpath', '//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_label);
    $this->assertSession()->elementExists('xpath', '//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]');
    $this->assertSession()->elementExists('xpath', '//div[@id="edit-' . $field_name . '-0--description"]');

    // Build up dates in the UTC timezone.     $value = '2012-12-31 00:00:00';
    $start_date = new DrupalDateTime($value, 'UTC');
    $end_value = '2013-06-06 00:00:00';
    $end_date = new DrupalDateTime($end_value, 'UTC');

    // Update the timezone to the system default.     $start_date->setTimezone(timezone_open(date_default_timezone_get()));
    $end_date->setTimezone(timezone_open(date_default_timezone_get()));

    // Submit a valid date and ensure it is accepted.     $date_format = DateFormat::load('html_date')->getPattern();
    $time_format = DateFormat::load('html_time')->getPattern();

    $edit = [
      "{$field_name}[0][value][date]" => $start_date->format($date_format),
      "{$field_name}[0][value][time]" => $start_date->format($time_format),
      "{$field_name}[0][end_value][date]" => $end_date->format($date_format),
      "{$field_name}[0][end_value][time]" => $end_date->format($time_format),
    ];

function wp_timezone_override_offset() {
    $timezone_string = get_option( 'timezone_string' );
    if ( ! $timezone_string ) {
        return false;
    }

    $timezone_object = timezone_open( $timezone_string );
    $datetime_object = date_create();
    if ( false === $timezone_object || false === $datetime_object ) {
        return false;
    }
    return round( timezone_offset_get( $timezone_object$datetime_object ) / HOUR_IN_SECONDS, 2 );
}

/** * Sort-helper for timezones. * * @since 2.9.0 * @access private * * @param array $a * @param array $b * @return int */

  protected function setTimeZone(DrupalDateTime $date) {
    if ($this->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
      // A date without time has no timezone conversion.       $timezone = DateTimeItemInterface::STORAGE_TIMEZONE;
    }
    else {
      $timezone = date_default_timezone_get();
    }
    $date->setTimeZone(timezone_open($timezone));
  }

  /** * Gets a settings array suitable for DrupalDateTime::format(). * * @return array * The settings array that can be passed to DrupalDateTime::format(). */
  protected function getFormatSettings() {
    $settings = [];

    
/** * {@inheritdoc} */
  public function format($timestamp$type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
    if (!isset($timezone)) {
      $timezone = date_default_timezone_get();
    }
    // Store DateTimeZone objects in an array rather than repeatedly     // constructing identical objects over the life of a request.     if (!isset($this->timezones[$timezone])) {
      $this->timezones[$timezone] = timezone_open($timezone);
    }

    if (empty($langcode)) {
      $langcode = $this->languageManager->getCurrentLanguage()->getId();
    }

    // Create a DrupalDateTime object from the timestamp and timezone.     $create_settings = [
      'langcode' => $langcode,
      'country' => $this->country(),
    ];
    

        } catch (PDOException $e) {
        }

        if (!empty($timeZone)) {
            $databaseZone = null;
            if (\in_array($timeZone[0]['-', '+'], true)) {
                $databaseZone = new DateTimeZone($timeZone);
            } else {
                $timeZoneFromAbbr = timezone_name_from_abbr($timeZone);
                if (\is_string($timeZoneFromAbbr)) {
                    $databaseZone = timezone_open($timeZoneFromAbbr);
                    if (!$databaseZone instanceof DateTimeZone) {
                        $databaseZone = null;
                    }
                }
            }

            $phpZone = timezone_open(date_default_timezone_get());
            if ($databaseZone instanceof DateTimeZone && $phpZone instanceof DateTimeZone) {
                $databaseTime = new DateTime('now', $databaseZone);
                $offset = abs($databaseZone->getOffset(new DateTime()) - $phpZone->getOffset($databaseTime));
            }
        }
$this->assertSession()->fieldValueEquals("{$field_name}[0][value][date]", '');
    $this->assertSession()->fieldValueEquals("{$field_name}[0][value][time]", '');
    $this->assertSession()->elementTextContains('xpath', '//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_label);
    $this->assertSession()->elementExists('xpath', '//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]');
    $this->assertSession()->elementExists('xpath', '//div[@id="edit-' . $field_name . '-0--description"]');

    // Build up a date in the UTC timezone.     $value = '2012-12-31 00:00:00';
    $date = new DrupalDateTime($value, 'UTC');

    // Update the timezone to the system default.     $date->setTimezone(timezone_open(date_default_timezone_get()));

    // Submit a valid date and ensure it is accepted.     $date_format = DateFormat::load('html_date')->getPattern();
    $time_format = DateFormat::load('html_time')->getPattern();

    $edit = [
      "{$field_name}[0][value][date]" => $date->format($date_format),
      "{$field_name}[0][value][time]" => $date->format($time_format),
    ];
    $this->submitForm($edit, 'Save');
    preg_match('|entity_test/manage/(\d+)|', $this->getUrl()$match);
    


  /** * Tests the "datetime_timestamp" widget. */
  public function testWidget() {
    // Build up a date in the UTC timezone.     $value = '2012-12-31 00:00:00';
    $date = new DrupalDateTime($value, 'UTC');

    // Update the timezone to the system default.     $date->setTimezone(timezone_open(date_default_timezone_get()));

    // Display creation form.     $this->drupalGet('entity_test/add');

    // Make sure the field description is properly displayed.     $this->assertSession()->pageTextContains('Description for timestamp field.');

    // Make sure the "datetime_timestamp" widget is on the page.     $this->assertSession()->elementsCount('xpath', '//div[contains(@class, "field--widget-datetime-timestamp") and @id="edit-field-timestamp-wrapper"]', 1);

    // Look for the widget elements and make sure they are empty.
$dst = (bool) $now->format( 'I' );

    if ( $dst ) {
        _e( 'This timezone is currently in daylight saving time.' );
    } else {
        _e( 'This timezone is currently in standard time.' );
    }
    ?> <br /> <?php     if ( in_array( $tzstringtimezone_identifiers_list( DateTimeZone::ALL_WITH_BC ), true ) ) {
        $transitions = timezone_transitions_get( timezone_open( $tzstring )time() );

        // 0 index is the state at current time, 1 index is the next transition, if any.         if ( ! empty( $transitions[1] ) ) {
            echo ' ';
            $message = $transitions[1]['isdst'] ?
                /* translators: %s: Date and time. */
                __( 'Daylight saving time begins on: %s.' ) :
                /* translators: %s: Date and time. */
                __( 'Standard time begins on: %s.' );
            printf(
                $message,
                

    public function setTimezone($zone = null)
    {
        $oldzone = @date_default_timezone_get();
        if ($zone === null) {
            $zone = $oldzone;
        }

        // throw an error on false input, but only if the new date extension is available         if (function_exists('timezone_open')) {
            if (!@timezone_open($zone)) {
                throw new Zend_Date_Exception("timezone ($zone) is not a known timezone", 0, null, $zone);
            }
        }
        // this can generate an error if the date extension is not available and a false timezone is given         $result = @date_default_timezone_set($zone);
        if ($result === true) {
            $this->_offset   = mktime(0, 0, 0, 1, 2, 1970) - gmmktime(0, 0, 0, 1, 2, 1970);
            $this->_timezone = $zone;
        }
        date_default_timezone_set($oldzone);

        
Home | Imprint | This part of the site doesn't use cookies.