Browse Source

Partial fix #273 Some PHP locales don't add AM/PM correctly

and some use a different case.

This also helps behat tests as they expect case to be correct.
MOODLE_34_STABLE
Dan Marsden 7 years ago
parent
commit
cb8a98ddbb
  1. 29
      renderhelpers.php

29
renderhelpers.php

@ -317,10 +317,35 @@ class user_sessions_cells_text_generator extends user_sessions_cells_generator {
function attendance_strftimehm($time) { function attendance_strftimehm($time) {
$mins = userdate($time, '%M'); $mins = userdate($time, '%M');
if ($mins == '00') { if ($mins == '00') {
return userdate($time, get_string('strftimeh', 'attendance')); $format = get_string('strftimeh', 'attendance');
} else { } else {
return userdate($time, get_string('strftimehm', 'attendance')); $format = get_string('strftimehm', 'attendance');
} }
$userdate = userdate($time, $format);
// Some Lang packs use %p to suffix with AM/PM but not all strftime support this.
// Check if %p is in use and make sure it's being respected.
if (stripos($format, '%p')) {
// Check if $userdate did something with %p by checking userdate against the same format without %p
$formatwithoutp = str_ireplace('%p', '', $format);
if (userdate($time, $formatwithoutp) == $userdate) {
// The date is the same with and without %p - we have a problem.
if (userdate($time, '%H') > 11) {
$userdate .= 'pm';
} else {
$userdate .= 'am';
}
}
// Some locales and O/S don't respect correct intended case of %p vs %P
// This can cause problems with behat which expects AM vs am.
if (strpos($format, '%p')) { // Should be upper case according to PHP spec.
$userdate = str_replace('am', 'AM', $userdate);
$userdate = str_replace('pm', 'PM', $userdate);
}
}
return $userdate;
} }
/** /**

Loading…
Cancel
Save