class JalaliDateFormatter in PersianTools 8
Overrides the drupal date service to provide a jalali date.
Hierarchy
- class \Drupal\Core\Datetime\DateFormatter implements DateFormatterInterface uses StringTranslationTrait
- class \Drupal\jalalidate\JalaliDateFormatter
Expanded class hierarchy of JalaliDateFormatter
1 string reference to 'JalaliDateFormatter'
- jalalidate.services.yml in jalalidate/
jalalidate.services.yml - jalalidate/jalalidate.services.yml
1 service uses JalaliDateFormatter
File
- jalalidate/
src/ JalaliDateFormatter.php, line 15 - Contains \Drupal\jalalidate\JalaliDate.
Namespace
Drupal\jalalidateView source
class JalaliDateFormatter extends DateFormatter {
/**
* {@inheritdoc}
*/
public function format($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
if (empty($langcode)) {
$langcode = $this->languageManager
->getCurrentLanguage()
->getId();
}
if ($langcode != "fa" || !class_exists('IntlDateFormatter')) {
return parent::format($timestamp, $type, $format, $timezone, $langcode);
}
if (!isset($timezone)) {
$timezone = date_default_timezone_get();
}
// 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 we have a non-custom date format use the provided date format pattern.
if ($date_format = $this
->dateFormat($type, $langcode)) {
switch ($date_format) {
case 'short':
$format = 'Y/M/d';
break;
case 'medium':
$format = 'Y/M/d - H:m';
break;
case 'long':
$format = 'EEEE Y/M/d - H:m';
break;
}
}
// Fall back to medium if a format was not found.
if (empty($format)) {
$format = 'EEEE Y/M/d - H:m';
}
$date = new \IntlDateFormatter("fa_IR@calendar=persian", \IntlDateFormatter::FULL, \IntlDateFormatter::SHORT, $timezone, \IntlDateFormatter::TRADITIONAL);
$date
->setPattern($format);
return $date
->format(intval($timestamp));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DateFormatter:: |
protected | property | The configuration factory. | |
DateFormatter:: |
protected | property | ||
DateFormatter:: |
protected | property | ||
DateFormatter:: |
protected | property | The date format storage. | |
DateFormatter:: |
protected | property | Language manager for retrieving the default langcode when none is specified. | |
DateFormatter:: |
protected | property | The request stack. | |
DateFormatter:: |
protected | property | The list of loaded timezones. | |
DateFormatter:: |
protected | property | Contains the different date interval units. | |
DateFormatter:: |
protected | function | Returns the default country from config. | |
DateFormatter:: |
protected | function | Loads the given format pattern for the given langcode. | |
DateFormatter:: |
public | function |
Formats a time interval between two timestamps. Overrides DateFormatterInterface:: |
|
DateFormatter:: |
public | function |
Formats a time interval with the requested granularity. Overrides DateFormatterInterface:: |
|
DateFormatter:: |
public | function |
Formats the time difference from a timestamp to the current request time. Overrides DateFormatterInterface:: |
|
DateFormatter:: |
public | function |
Formats the time difference from the current request time to a timestamp. Overrides DateFormatterInterface:: |
|
DateFormatter:: |
public | function |
Provides values for all date formatting characters for a given timestamp. Overrides DateFormatterInterface:: |
|
DateFormatter:: |
public | function | Constructs a Date object. | |
JalaliDateFormatter:: |
public | function |
Formats a date, using a date type or a custom date format string. Overrides DateFormatter:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |