getDateFormat example

/** * Tests date formatting replacement. * * @covers ::getDateFormat * * @dataProvider providerTestGetDateFormat */
  public function testGetDateFormat($field$format$expected) {
    $date_sql = new SqliteDateSql($this->database);

    $this->assertEquals($expected$date_sql->getDateFormat($field$format));
  }

  /** * Provider for date formatting test. */
  public function providerTestGetDateFormat() {
    return [
      ['foo.field', 'Y-y-M-m', "strftime('%Y-%Y-%m-%m', foo.field, 'unixepoch')"],
      ['bar.field', 'n-F D d l', "strftime('%m-%m %d %d %d', bar.field, 'unixepoch')"],
      ['baz.bar_field', 'j/W/H-h i s A', "strftime('%d/%W/%H-%H %M %S ', baz.bar_field, 'unixepoch')"],
      ['foo.field', 'W', "CAST(((strftime('%j', date(strftime('%Y-%m-%d', foo.field, 'unixepoch'), '-3 days', 'weekday 4')) - 1) / 7 + 1) AS NUMERIC)"],
    ];
/** * {@inheritdoc} */
  public function getSortName() {
    return $this->t('Date', []['context' => 'Sort order']);
  }

  /** * {@inheritdoc} */
  public function getFormula() {
    $this->formula = $this->getDateFormat($this->argFormat);
    return parent::getFormula();
  }

}
/** * Tests date formatting replacement. * * @covers ::getDateFormat * * @dataProvider providerTestGetDateFormat */
  public function testGetDateFormat($field$format$expected_format) {
    $date_sql = new MysqlDateSql($this->database);

    $this->assertEquals("DATE_FORMAT($field, '$expected_format')", $date_sql->getDateFormat($field$format));
  }

  /** * Provider for date formatting test. */
  public function providerTestGetDateFormat() {
    return [
      ['foo.field', 'Y-y-M-m', '%Y-%y-%b-%m'],
      ['bar.field', 'n-F D d l', '%c-%M %a %d %W'],
      ['baz.bar_field', 'o j/W/H-h i s A', '%x %e/%v/%H-%h %i %s %p'],
    ];
  }
/** * Creates cross-database SQL date formatting. * * @param string $format * A format string for the result, like 'Y-m-d H:i:s'. * * @return string * An appropriate SQL string for the DB type and field type. */
  public function getDateFormat($format) {
    return $this->query->getDateFormat($this->getDateField()$format);
  }

  /** * Creates cross-database SQL dates. * * @return string * An appropriate SQL string for the db type and field type. */
  public function getDateField() {
    return $this->query->getDateField("$this->tableAlias.$this->realField");
  }

  
continue;
            }
            $this->twig->addExtension($extension);
        }
        if ($this->twig->hasExtension(CoreExtension::class) && $this->platformTwig->hasExtension(CoreExtension::class)) {
            /** @var CoreExtension $coreExtensionInternal */
            $coreExtensionInternal = $this->twig->getExtension(CoreExtension::class);
            /** @var CoreExtension $coreExtensionGlobal */
            $coreExtensionGlobal = $this->platformTwig->getExtension(CoreExtension::class);

            $coreExtensionInternal->setTimezone($coreExtensionGlobal->getTimezone());
            $coreExtensionInternal->setDateFormat(...$coreExtensionGlobal->getDateFormat());
            $coreExtensionInternal->setNumberFormat(...$coreExtensionGlobal->getNumberFormat());
        }
    }

    /** * @param array<string, mixed> $data * * @throws StringTemplateRenderingException */
    public function render(string $templateSource, array $data, Context $context, bool $htmlEscape = true): string
    {
        
// Use string date storage/formatting since datetime fields are stored as     // strings rather than UNIX timestamps.     return $this->query->getDateField("$this->tableAlias.$this->realField", TRUE, $this->calculateOffset);
  }

  /** * {@inheritdoc} * * Overridden in order to pass in the string date flag. */
  public function getDateFormat($format) {
    return $this->query->getDateFormat($this->getDateField()$format, TRUE);
  }

}
/** * Tests date formatting replacement. * * @covers ::getDateFormat * * @dataProvider providerTestGetDateFormat */
  public function testGetDateFormat($field$format$expected_format) {
    $date_sql = new PostgresqlDateSql($this->database);

    $this->assertEquals("TO_CHAR($field, '$expected_format')", $date_sql->getDateFormat($field$format));
  }

  /** * Provider for date formatting test. */
  public function providerTestGetDateFormat() {
    return [
      ['foo.field', 'Y-y-M-m', 'YYYY-YY-Mon-MM'],
      ['bar.field', 'n-F D d l', 'MM-Month Dy DD Day'],
      ['baz.bar_field', 'j/W/H-h i s A', 'DD/IW/HH24-HH12 MI SS AM'],
    ];
  }

function twig_date_format_filter(Environment $env$date$format = null, $timezone = null)
{
    if (null === $format) {
        $formats = $env->getExtension(CoreExtension::class)->getDateFormat();
        $format = $date instanceof \DateInterval ? $formats[1] : $formats[0];
    }

    if ($date instanceof \DateInterval) {
        return $date->format($format);
    }

    return twig_date_converter($env$date$timezone)->format($format);
}

/** * Returns a new date object modified. * * {{ post.published_at|date_modify("-1day")|date("m/d/Y") }} * * @param \DateTimeInterface|string $date A date * @param string $modifier A modifier string * * @return \DateTimeInterface */
$this->dateSql->setTimezoneOffset('+00:00');
      $already_set = TRUE;
    }

    return parent::setupTimezone();
  }

  /** * {@inheritdoc} */
  public function getDateFormat($field$format$string_date = FALSE) {
    return $this->dateSql->getDateFormat($field$format);
  }

}
public function getDateField() {
    // Use string date storage/formatting since datetime fields are stored as     // strings rather than UNIX timestamps.     return $this->query->getDateField("$this->tableAlias.$this->realField", TRUE, $this->calculateOffset);
  }

  /** * {@inheritdoc} */
  public function getDateFormat($format) {
    // Pass in the string-field option.     return $this->query->getDateFormat($this->getDateField()$format, TRUE);
  }

}
protected function opBetween($field) {
    $timezone = $this->getTimezone();
    $origin_offset = $this->getOffset($this->value['min']$timezone);

    // Although both 'min' and 'max' values are required, default empty 'min'     // value as UNIX timestamp 0.     $min = (!empty($this->value['min'])) ? $this->value['min'] : '@0';

    // Convert to ISO format and format for query. UTC timezone is used since     // dates are stored in UTC.     $a = new DateTimePlus($minnew \DateTimeZone($timezone));
    $a = $this->query->getDateFormat($this->query->getDateField("'" . $this->dateFormatter->format($a->getTimestamp() + $origin_offset, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE) . "'", TRUE, $this->calculateOffset)$this->dateFormat, TRUE);
    $b = new DateTimePlus($this->value['max']new \DateTimeZone($timezone));
    $b = $this->query->getDateFormat($this->query->getDateField("'" . $this->dateFormatter->format($b->getTimestamp() + $origin_offset, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE) . "'", TRUE, $this->calculateOffset)$this->dateFormat, TRUE);

    // This is safe because we are manually scrubbing the values.     $operator = strtoupper($this->operator);
    $field = $this->query->getDateFormat($this->query->getDateField($field, TRUE, $this->calculateOffset)$this->dateFormat, TRUE);
    $this->query->addWhereExpression($this->options['group'], "$field $operator $a AND $b");
  }

  /** * Override parent method, which deals with dates as integers. */
throw new Zend_Locale_Exception("Unknown number format type '" . gettype($value) . "'. "
                            . "Format '$stringValue' must be a valid number format string.");
                    }
                    break;

                case 'date_format' :
                    if ($value == Zend_Locale_Format::STANDARD) {
                        $locale = self::$_options['locale'];
                        if (isset($options['locale'])) {
                            $locale = $options['locale'];
                        }
                        $options['date_format'] = Zend_Locale_Format::getDateFormat($locale);
                    } else if ((gettype($value) !== 'string') and ($value !== NULL)) {
                        $stringValue = (string)(is_array($value) ? implode(' ', $value) : $value);
                        throw new Zend_Locale_Exception("Unknown dateformat type '" . gettype($value) . "'. "
                            . "Format '$stringValue' must be a valid ISO or PHP date format string.");
                    } else {
                        if (((isset($options['format_type']) === true) and ($options['format_type'] == 'php')) or
                            ((isset($options['format_type']) === false) and (self::$_options['format_type'] == 'php'))) {
                            $options['date_format'] = Zend_Locale_Format::convertPhpToIsoFormat($value);
                        }
                    }
                    break;

                

  public function query() {
    $this->ensureMyTable();
    switch ($this->options['granularity']) {
      case 'second':
      default:
        $this->query->addOrderBy($this->tableAlias, $this->realField, $this->options['order']);
        return;

      case 'minute':
        $formula = $this->getDateFormat('YmdHi');
        break;

      case 'hour':
        $formula = $this->getDateFormat('YmdH');
        break;

      case 'day':
        $formula = $this->getDateFormat('Ymd');
        break;

      case 'month':
        
if (($type !== null) and ($type != 'php') and ($type != 'iso') and
            Zend_Locale::isLocale($type, null, false)) {
            $locale = $type;
            $type = null;
        }

        if ($locale === null) {
            $locale = $this->getLocale();
        }

        if ($format === null) {
            $format = Zend_Locale_Format::getDateFormat($locale) . ' ' . Zend_Locale_Format::getTimeFormat($locale);
        } else if (((self::$_options['format_type'] == 'php') && ($type === null)) or ($type == 'php')) {
            $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
        }

        return $this->date($this->_toToken($format$locale)$this->getUnixTimestamp(), false);
    }

    /** * Returns a string representation of the date which is equal with the timestamp * * @return string */
Home | Imprint | This part of the site doesn't use cookies.