formatInterval example

'#description' => $this->t('How many different units to display in the string.'),
      '#default_value' => $this->options['granularity'],
    ];
  }

  /** * {@inheritdoc} */
  public function render(ResultRow $values) {
    $value = $values->{$this->field_alias};
    if ($value != NULL) {
      return $this->dateFormatter->formatInterval((int) $value$this->options['granularity'] ?? 2);
    }
    return '';
  }

}

        $productFeedRepository = $this->container->get(\Shopware\Components\Model\ModelManager::class)->getRepository('Shopware\Models\ProductFeed\ProductFeed');
        $productFeeds = $productFeedRepository->findAll();

        $rows = [];
        /** @var ProductFeed $productFeed */
        foreach ($productFeeds as $productFeed) {
            $rows[] = [
                $productFeed->getName(),
                $productFeed->getId(),
                $productFeed->getLastExport()->format('Y-m-d H:i:s'),
                $this->formatInterval($productFeed->getInterval()),
                $productFeed->getActive() ? 'Yes' : 'No',
            ];
        }

        $table = new Table($output);
        $table->setHeaders(['Product Feed', 'Id', 'Last export', 'Interval', 'Active'])
            ->setRows($rows);

        $table->render();

        return 0;
    }
/** * @return array */
    public static function castDateTime(\DateTimeInterface $d, array $a, Stub $stub, bool $isNested, int $filter)
    {
        $prefix = Caster::PREFIX_VIRTUAL;
        $location = $d->getTimezone() ? $d->getTimezone()->getLocation() : null;
        $fromNow = (new \DateTimeImmutable())->diff($d);

        $title = $d->format('l, F j, Y')
            ."\n".self::formatInterval($fromNow).' from now'
            .($location ? ($d->format('I') ? "\nDST On" : "\nDST Off") : '')
        ;

        unset(
            $a[Caster::PREFIX_DYNAMIC.'date'],
            $a[Caster::PREFIX_DYNAMIC.'timezone'],
            $a[Caster::PREFIX_DYNAMIC.'timezone_type']
        );
        $a[$prefix.'date'] = new ConstStub(self::formatDateTime($d$location ? ' e (P)' : ' P')$title);

        $stub->class .= $d->format(' @U');

        

  public function testFormatInterval($interval$granularity$expected$langcode = NULL) {
    // Mocks a simple formatPlural implementation.     $this->stringTranslation->expects($this->any())
      ->method('translateString')
      ->willReturnCallback(function DTranslatableMarkup $arg) {
        return $arg->getUntranslatedString();
      });

    // Check if the granularity is specified.     if ($granularity) {
      $result = $this->dateFormatter->formatInterval($interval$granularity$langcode);
    }
    else {
      $result = $this->dateFormatter->formatInterval($interval);
    }

    $this->assertEquals(new TranslatableMarkup($expected[]['langcode' => $langcode]$this->stringTranslation)$result);
  }

  /** * Provides some test data for the format interval test. */
  
/** * @return array */
    public static function castDateTime(\DateTimeInterface $d, array $a, Stub $stub, bool $isNested, int $filter)
    {
        $prefix = Caster::PREFIX_VIRTUAL;
        $location = $d->getTimezone() ? $d->getTimezone()->getLocation() : null;
        $fromNow = (new \DateTimeImmutable())->diff($d);

        $title = $d->format('l, F j, Y')
            ."\n".self::formatInterval($fromNow).' from now'
            .($location ? ($d->format('I') ? "\nDST On" : "\nDST Off") : '')
        ;

        unset(
            $a[Caster::PREFIX_DYNAMIC.'date'],
            $a[Caster::PREFIX_DYNAMIC.'timezone'],
            $a[Caster::PREFIX_DYNAMIC.'timezone_type']
        );
        $a[$prefix.'date'] = new ConstStub(self::formatDateTime($d$location ? ' e (P)' : ' P')$title);

        $stub->class .= $d->format(' @U');

        
public function validateForm(array &$form, FormStateInterface $form_state) {
    $message = parent::validateForm($form$form_state);

    // Check if flood control has been activated for sending emails.     if (!$this->currentUser()->hasPermission('administer contact forms') && (!$message->isPersonal() || !$this->currentUser()->hasPermission('administer users'))) {
      $limit = $this->config('contact.settings')->get('flood.limit');
      $interval = $this->config('contact.settings')->get('flood.interval');

      if (!$this->flood->isAllowed('contact', $limit$interval)) {
        $form_state->setErrorByName('', $this->t('You cannot send more than %limit messages in @interval. Try again later.', [
          '%limit' => $limit,
          '@interval' => $this->dateFormatter->formatInterval($interval),
        ]));
      }
    }

    return $message;
  }

  /** * {@inheritdoc} */
  public function save(array $form, FormStateInterface $form_state) {
    
$this->setCommentSubject(TRUE);
    $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');

    $edit = [];
    $date = new DrupalDateTime('2008-03-02 17:23');
    $edit['subject[0][value]'] = $this->randomMachineName(8);
    $edit['comment_body[0][value]'] = $this->randomMachineName(16);
    $edit['uid'] = $web_user->getAccountName() . ' (' . $web_user->id() . ')';
    $edit['date[date]'] = $date->format('Y-m-d');
    $edit['date[time]'] = $date->format('H:i:s');
    $raw_date = $date->getTimestamp();
    $expected_text_date = $this->container->get('date.formatter')->formatInterval(\Drupal::time()->getRequestTime() - $raw_date);
    $expected_form_date = $date->format('Y-m-d');
    $expected_form_time = $date->format('H:i:s');
    $comment = $this->postComment($this->node, $edit['subject[0][value]']$edit['comment_body[0][value]'], TRUE);
    $this->drupalGet('comment/' . $comment->id() . '/edit');
    $this->submitForm($edit, 'Preview');

    // Check that the preview is displaying the subject, comment, author and date correctly.     $this->assertSession()->titleEquals('Preview comment | Drupal');
    $this->assertSession()->pageTextContains($edit['subject[0][value]']);
    $this->assertSession()->pageTextContains($edit['comment_body[0][value]']);
    $this->assertSession()->pageTextContains($web_user->getAccountName());
    
foreach ($custom_fields as $field) {
      $cache_options = $form_state->getValue('cache_options');
      if ($cache_options[$field] == 'custom' && !is_numeric($cache_options[$field . '_custom'])) {
        $form_state->setError($form[$field . '_custom']$this->t('Custom time values must be numeric.'));
      }
    }
  }

  public function summaryTitle() {
    $results_lifespan = $this->getLifespan('results');
    $output_lifespan = $this->getLifespan('output');
    return $this->dateFormatter->formatInterval($results_lifespan, 1) . '/' . $this->dateFormatter->formatInterval($output_lifespan, 1);
  }

  protected function getLifespan($type) {
    $lifespan = $this->options[$type . '_lifespan'] == 'custom' ? $this->options[$type . '_lifespan_custom'] : $this->options[$type . '_lifespan'];
    return $lifespan;
  }

  protected function cacheExpire($type) {
    $lifespan = $this->getLifespan($type);
    if ($lifespan) {
      $cutoff = REQUEST_TIME - $lifespan;
      
$this->drupalLogin($this->webUser);

    // Submit contact form with correct values and check flood interval.     for ($i = 0; $i < $flood_limit$i++) {
      $this->submitPersonalContact($this->contactUser);
      $this->assertSession()->pageTextContains('Your message has been sent.');
    }

    // Submit contact form one over limit.     $this->submitPersonalContact($this->contactUser);
    // Normal user should be denied access to flooded contact form.     $interval = \Drupal::service('date.formatter')->formatInterval($this->config('contact.settings')->get('flood.interval'));
    $this->assertSession()->pageTextContains("You cannot send more than 3 messages in {$interval}. Try again later.");

    // Test that the admin user can still access the contact form even though     // the flood limit was reached.     $this->drupalLogin($this->adminUser);
    $this->assertSession()->pageTextNotContains('Try again later.');
  }

  /** * Tests the personal contact form based access when an admin adds users. */
  
Home | Imprint | This part of the site doesn't use cookies.