You are here

public function PersianDateFormatter::format in Persian Date for Drupal 8 8.4

Same name and namespace in other branches
  1. 8 src/Service/Formatter/PersianDateFormatter.php \Drupal\persian_date\Service\Formatter\PersianDateFormatter::format()

Formats a date, using a date type or a custom date format string.

Parameters

int $timestamp: A UNIX timestamp to format.

string $type: (optional) The format to use, one of:

  • One of the built-in formats: 'short', 'medium', 'long', 'html_datetime', 'html_date', 'html_time', 'html_yearless_date', 'html_week', 'html_month', 'html_year'.
  • The name of a date type defined by a date format config entity.
  • The machine name of an administrator-defined date format.
  • 'custom', to use $format.

Defaults to 'medium'.

string $format: (optional) If $type is 'custom', a PHP date format string suitable for input to date(). Use a backslash to escape ordinary text, so it does not get interpreted as date format characters.

string|null $timezone: (optional) Time zone identifier, as described at http://php.net/manual/timezones.php Defaults to the time zone used to display the page.

string|null $langcode: (optional) Language code to translate to. NULL (default) means to use the user interface language for the page.

Return value

string A translated date string in the requested format. Since the format may contain user input, this value should be escaped when output.

Overrides DateFormatter::format

File

src/Service/Formatter/PersianDateFormatter.php, line 12

Class

PersianDateFormatter

Namespace

Drupal\persian_date\Service\Formatter

Code

public function format($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {

  // return default formatter if website is not persian
  // or caller needs machine-readable data
  if (!PersianLanguageDiscovery::isPersian() || $this
    ->isForMetaTag()) {
    return parent::format($timestamp, $type, $format, $timezone, $langcode);
  }
  if (!isset($timezone)) {
    $timezone = drupal_get_user_timezone();
  }

  // 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();
  }
  $date = PersianDateFactory::buildFromTimestamp($timestamp, $this->timezones[$timezone]);

  // If we have a non-custom date format use the provided date format pattern.
  if ($type !== 'custom') {
    if ($date_format = $this
      ->dateFormat($type, $langcode)) {
      $format = $date_format
        ->getPattern();
    }
  }

  // called by query builder
  if ($type === 'custom' & $format === DATETIME_DATETIME_STORAGE_FORMAT) {

    // convert shamsi to georgian
    $date = parent::format($timestamp, $type, $format, $timezone, $langcode);
    $year = explode('-', $date)[0];

    // if date is shamsi
    if (!is_georgian_year($year)) {
      $other = PersianDateConverter::normalizeDate(new \DateTime($date));
      $date = parent::format($other
        ->getTimestamp(), $type, $format, $timezone, $langcode);
    }
    return $date;
  }

  // Fall back to the 'medium' date format type if the format string is
  // empty, either from not finding a requested date format or being given an
  // empty custom format string.
  if (empty($format)) {
    $format = $this
      ->dateFormat('fallback', $langcode)
      ->getPattern();
  }
  return $date
    ->format($format);
}