public function ApStyleDateFormatter::formatTimestamp in AP Style Date 8
Format a timestamp to an AP style date format.
Parameters
int $timestamp: The timestamp to convert.
array $options: An array of options that affect how the date string is formatted.
mixed $timezone: \DateTimeZone object, time zone string or NULL. NULL uses the default system time zone. Defaults to NULL.
string $langcode: The language code.
Return value
string The formatted date string.
File
- src/
ApStyleDateFormatter.php, line 115
Class
- ApStyleDateFormatter
- Services for formatting date types using AP Style Date rules.
Namespace
Drupal\date_ap_styleCode
public function formatTimestamp($timestamp, array $options = [], $timezone = NULL, $langcode = NULL) {
if (empty($options)) {
$options = $this
->getOptions();
}
if (empty($langcode)) {
$langcode = $this->languageManager
->getCurrentLanguage()
->getId();
}
// If no timezone is specified, use the user's if available, or the site
// or system default.
if (empty($timezone)) {
$timezone = date_default_timezone_get();
}
// Create a DrupalDateTime object from the timestamp and timezone.
$datetime_settings = [
'langcode' => $langcode,
];
$date_string = '';
$format_date = '';
// Create a DrupalDateTime object from the timestamp and timezone.
$date = DrupalDateTime::createFromTimestamp($timestamp, $timezone, $datetime_settings);
$now = new DrupalDateTime('now', $timezone, $datetime_settings);
if (isset($options['use_today']) && $options['use_today'] && $date
->format('Y-m-d') == $now
->format('Y-m-d')) {
$date_string = $this
->t('today');
if (isset($options['cap_today']) && $options['cap_today']) {
$date_string = ucfirst($date_string);
}
}
elseif (isset($options['display_day']) && $options['display_day'] && $date
->format('W o') == $now
->format('W o')) {
$format_date .= 'l';
}
else {
$format_date .= $this
->formatMonth($date) . ' j';
if (isset($options['always_display_year']) && $options['always_display_year'] || $date
->format('Y') != $now
->format('Y')) {
$format_date .= ', Y';
}
}
$date_string .= $date
->format($format_date);
if (isset($options['display_time']) && $options['display_time']) {
$capital = isset($options['capitalize_noon_and_midnight']) && $options['capitalize_noon_and_midnight'];
switch ($date
->format('H:i')) {
case '00:00':
if (isset($options['use_all_day']) && $options['use_all_day']) {
$ap_time_string = $this
->t('All Day');
}
else {
$ap_time_string = $this
->t('midnight');
if ($capital) {
$ap_time_string = ucfirst($ap_time_string);
}
}
break;
case '12:00':
$ap_time_string = $this
->t('noon');
if ($capital) {
$ap_time_string = ucfirst($ap_time_string);
}
break;
default:
if ($date
->format('i') === '00') {
// Don't display the minutes if it's the top of the hour.
$ap_time_string = $date
->format('g a');
}
else {
$ap_time_string = $date
->format('g:i a');
}
break;
}
// Format the meridian if it's there.
$ap_time_string = str_replace([
'am',
'pm',
], [
'a.m.',
'p.m.',
], $ap_time_string);
if (isset($options['time_before_date']) && $options['time_before_date']) {
$output = $ap_time_string . ', ' . $date_string;
}
else {
$output = $date_string . ', ' . $ap_time_string;
}
}
else {
$output = $date_string;
}
return $output;
}