formatTimeDiffUntil example

$component['settings'] = $settings;
      $this->display->setComponent($this->fieldName, $component);

      $entity = EntityTest::create([]);
      $entity->{$this->fieldName}->value = $value;

      $this->renderEntityFields($entity$this->display);
      $this->assertRaw($expected);

      // Test a timestamp in the future       $value = $request_time + 87654321;
      $expected = new FormattableMarkup($future_format['@interval' => \Drupal::service('date.formatter')->formatTimeDiffUntil($value['granularity' => $granularity])]);

      $component = $this->display->getComponent($this->fieldName);
      $component['type'] = 'timestamp_ago';
      $component['settings'] = $settings;
      $this->display->setComponent($this->fieldName, $component);

      $entity = EntityTest::create([]);
      $entity->{$this->fieldName}->value = $value;

      $this->renderEntityFields($entity$this->display);
      $this->assertRaw($expected);
    }
// Will be positive for a datetime in the past (ago), and negative for a       // datetime in the future (hence).       $time_diff = REQUEST_TIME - $value;
      switch ($format) {
        case 'raw time ago':
          return $this->dateFormatter->formatTimeDiffSince($value['granularity' => is_numeric($custom_format) ? $custom_format : 2]);

        case 'time ago':
          return $this->t('%time ago', ['%time' => $this->dateFormatter->formatTimeDiffSince($value['granularity' => is_numeric($custom_format) ? $custom_format : 2])]);

        case 'raw time hence':
          return $this->dateFormatter->formatTimeDiffUntil($value['granularity' => is_numeric($custom_format) ? $custom_format : 2]);

        case 'time hence':
          return $this->t('%time hence', ['%time' => $this->dateFormatter->formatTimeDiffUntil($value['granularity' => is_numeric($custom_format) ? $custom_format : 2])]);

        case 'raw time span':
          return ($time_diff < 0 ? '-' : '') . $this->dateFormatter->formatTimeDiffSince($value['strict' => FALSE, 'granularity' => is_numeric($custom_format) ? $custom_format : 2]);

        case 'inverse time span':
          return ($time_diff > 0 ? '-' : '') . $this->dateFormatter->formatTimeDiffSince($value['strict' => FALSE, 'granularity' => is_numeric($custom_format) ? $custom_format : 2]);

        case 'time span':
          
$intervals = [
      'raw time ago' => $time_since,
      'time ago' => "$time_since ago",
      'raw time span' => $time_since,
      'inverse time span' => "-$time_since",
      'time span' => "$time_since ago",
    ];
    $this->assertRenderedDatesEqual($view$intervals);

    // Check times in the future.     $time = gmmktime(0, 0, 0, 1, 1, 2050);
    $formatted = $date_formatter->formatTimeDiffUntil($time);
    $intervals = [
      'raw time span' => "-$formatted",
      'time span' => "$formatted hence",
    ];
    $this->assertRenderedFutureDatesEqual($view$intervals);
  }

  /** * Asserts properly formatted display against 'created' field in view. * * @param \Drupal\views\ViewExecutable $view * View to be tested. * @param array $map * Data map. * @param string|null $timezone * Optional timezone. * * @internal */
$time_diff = $this->getSetting('time_diff');
    $date_format = $this->getSetting('date_format');
    $date_format = $date_format === static::CUSTOM_DATE_FORMAT ? $this->getSetting('custom_date_format') : $date_format;

    if ($time_diff['enabled']) {
      $summary[] = $this->t('Displayed as a time difference');

      $options = ['granularity' => $time_diff['granularity']];

      $timestamp = strtotime('1 year 1 month 1 week 1 day 1 hour 1 minute');
      $interval = $this->dateFormatter->formatTimeDiffUntil($timestamp$options);
      $display = new FormattableMarkup($time_diff['future_format']['@interval' => $interval]);
      $summary[] = $this->t('Future date: %display', ['%display' => $display]);

      $timestamp = strtotime('-1 year -1 month -1 week -1 day -1 hour -1 minute');
      $interval = $this->dateFormatter->formatTimeDiffSince($timestamp$options);
      $display = new FormattableMarkup($time_diff['past_format']['@interval' => $interval]);
      $summary[] = $this->t('Past date: %display', ['%display' => $display]);

      if ($time_diff['refresh']) {
        $refresh_intervals = $this->getRefreshIntervals();
        $summary[] = $this->t('Refresh every @interval', ['@interval' => $refresh_intervals[$time_diff['refresh']]]);
      }
public function settingsSummary() {
    $summary = parent::settingsSummary();

    $future_date = new DrupalDateTime('1 year 1 month 1 week 1 day 1 hour 1 minute');
    $past_date = new DrupalDateTime('-1 year -1 month -1 week -1 day -1 hour -1 minute');
    $granularity = $this->getSetting('granularity');
    $options = [
      'granularity' => $granularity,
      'return_as_object' => FALSE,
    ];

    $future_date_interval = new FormattableMarkup($this->getSetting('future_format')['@interval' => $this->dateFormatter->formatTimeDiffUntil($future_date->getTimestamp()$options)]);
    $past_date_interval = new FormattableMarkup($this->getSetting('past_format')['@interval' => $this->dateFormatter->formatTimeDiffSince($past_date->getTimestamp()$options)]);

    $summary[] = $this->t('Future date: %display', ['%display' => $future_date_interval]);
    $summary[] = $this->t('Past date: %display', ['%display' => $past_date_interval]);

    return $summary;
  }

  /** * {@inheritdoc} */
  
$request_time$timestamp$options$expected],
        [$request_time$timestamp$options + ['return_as_object' => TRUE]new FormattedDateDiff('1 second', 1)],
      ]);

    $request = Request::createFromGlobals();
    $request->server->set('REQUEST_TIME', $request_time);
    // Mocks a the request stack getting the current request.     $this->requestStack->expects($this->any())
      ->method('getCurrentRequest')
      ->willReturn($request);

    $this->assertEquals($expected$this->dateFormatterStub->formatTimeDiffUntil($timestamp$options));
    $options['return_as_object'] = TRUE;
    $expected_object = new FormattedDateDiff('1 second', 1);
    $this->assertEquals($expected_object$this->dateFormatterStub->formatTimeDiffUntil($timestamp$options));
  }

  /** * Tests the formatDiff method. * * @dataProvider providerTestFormatDiff * * @covers ::formatDiff */
      $timestamp = REQUEST_TIME + 87654321;
      $entity = EntityTest::load($id);
      $field_name = $this->fieldStorage->getName();
      $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
      $entity->{$field_name}->value = $date->format($date_format);
      $entity->save();

      $display_repository->getViewDisplay($this->field->getTargetEntityTypeId()$this->field->getTargetBundle(), 'full')
        ->setComponent($field_name$this->displayOptions)
        ->save();
      $expected = new FormattableMarkup($this->displayOptions['settings']['future_format'][
        '@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp['granularity' => $this->displayOptions['settings']['granularity']]),
      ]);
      $output = $this->renderTestEntity($id);
      $this->assertStringContainsString((string) $expected$outputnew FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected in %timezone.', [
        '%expected' => $expected,
        '%timezone' => $timezone,
      ]));
    }
  }

  /** * Tests date and time field. */
Home | Imprint | This part of the site doesn't use cookies.