With some locale settings, %p
returns an empty string. I would prefer in those cases to have it return either AM or PM even when that is not correct for the language.
The usage case is calendar of events where the user can choose to override the default and specify they want 12 hour time. It would make the code a lot easier if at the beginning of the class, I could detect if %p
returns a localized string or empty, and define it as AM or PM when empty.
-- EDIT --
<?php
setlocale(LC_TIME, 'ru_RU.UTF8');
print(strftime('%l:%M %p %Z', time()));
?>
In many locales, such as ru_RU
, %p
returns a useless empty string. When a translation for AM or PM is not defined, I want to define it as AM or PM. Just like my question before the edit specified.
First thing that comes to mind, and this may be more of a bandaid solution, but is to check in $dformat
for %p
EDITED SOLUTION
If there are varied forms of formatting as I gathered in the comments, than perhaps it's best to actually interpolate your own %p if %p renders an empty string so this solution may work for you:
$check_for_empty_string = false;
if (preg_match("@%p@", $dformat)) {
if (strftime('%p', $node->start) == '') {
// get 24 hours
$hour = strtotime('%H', $start);
if ($hour >= 12) {
// it's PM
$dformat = preg_replace("@%p@", $this->PM, $dformat);
} else {
$dformat = preg_replace("@%p@", $this->AM, $dformat);
}
}
}
$start = strftime($dformat, $node->start);
$search = array(
'/^12:00 ' . $this->AM . '/',
'/^00:00$/', '/^12:00 ' . $this->PM . '/',
'/^12:00$/'
);
$replace = array(
'12:00 ' . $this->getString('Midnight'),
'00:00 ' . $this->getString('Midnight'),
'12:00 ' . $this->getString('Noon'),
'12:00 ' . $this->getString('Noon')
);
$start = preg_replace($search, $replace, $start);
$event->setAttribute('start', $start);