FormattedDateDiff example

$expected = '1 second';
    $request_time = $this->createTimestamp('2013-12-11 10:09:08');
    $timestamp = $this->createTimestamp('2013-12-11 10:09:09');
    $options = [];

    // Mocks the formatDiff function of the dateformatter object.     $this->dateFormatterStub
      ->expects($this->exactly(2))
      ->method('formatDiff')
      ->willReturnMap([
        [$timestamp$request_time$options$expected],
        [$timestamp$request_time$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->formatTimeDiffSince($timestamp$options));
    $options['return_as_object'] = TRUE;
    
$options += [
      'granularity' => 2,
      'langcode' => NULL,
      'strict' => TRUE,
      'return_as_object' => FALSE,
    ];

    if ($options['strict'] && $from > $to) {
      $string = $this->t('0 seconds');
      if ($options['return_as_object']) {
        return new FormattedDateDiff($string, 0);
      }
      return $string;
    }

    $date_time_from = new \DateTime();
    $date_time_from->setTimestamp($from);

    $date_time_to = new \DateTime();
    $date_time_to->setTimestamp($to);

    $interval = $date_time_to->diff($date_time_from);

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