Calendar Widget

Consistent Day Name Abbreviation

This guide explains how to generate and display localized day names in the Calendar widget, ensuring consistent abbreviations across different locales (e.g., German, Japanese). The key idea is to always fetch the full day name with '%A' and then truncate it to the desired length using $day_name_length.

Why Not '%a'?

Implementation Steps

1. Fetch full day names

// Initialize day names array
$day_names = array();
for ($n = 0, $t = (3 + $first_day) * 86400; $n < 7; $n++, $t += 86400) {
    // '%A' always returns the full day name in current locale
    $day_names[$n] = ucfirst(date_strformat('%A', $t));
}

2. Output with controlled abbreviation

// Output table headers for weekdays
foreach ($day_names as $fullName) {
    // Truncate to $day_name_length characters
    if ($day_name_length > 0) {
        $label = mb_substr($fullName, 0, $day_name_length, $characterset);
    } else {
        // 0 means show full name
        $label = $fullName;
    }
    // Use the full name for the 'abbr' attribute
    $calendar .= '<th abbr="'.htmlentities($fullName).'">'
                .htmlentities($label).'</th>';
}

3. Adjust $day_name_length

Examples

German (locale="de-de", $day_name_length = 3):

Japanese (locale="ja-jp", $day_name_length = 1):

Benefits

Integration

Ensure $characterset matches your locale encoding and that set_locale() is called appropriately.