Change date number to another language/script?

I previously was able to change the numbers of the date and time (into a number and date system of another language, like Khmer) for each post using a script like this in my functions.php:

function KhmerNumDate ($text) {
$text = str_replace('1', '១', $text);
$text = str_replace('2', '២', $text);
$text = str_replace('3', '៣', $text);
$text = str_replace('4', '៤', $text);
$text = str_replace('5', '៥', $text);
$text = str_replace('6', '៦', $text);
$text = str_replace('7', '៧', $text);
$text = str_replace('8', '៨', $text);
$text = str_replace('9', '៩', $text);
$text = str_replace('0', '០', $text); 
return $text;
}


add_filter('date', 'KhmerNumDate');
add_filter('the_date', 'KhmerNumDate');
add_filter('the_time', 'KhmerNumDate');

But now it is not working – is my code good? Would the code differ based on which theme I am using (I am currently using a modified child theme of Twenty-Twelve)?

Related posts

Leave a Reply

3 comments

  1. Please try this way:

    Change date, the_date, the_time to get_date, get_the_date, get_the_time.

    function KhmerNumDate ($text) {
        $text = str_replace('1', '១', $text);
        $text = str_replace('2', '២', $text);
        $text = str_replace('3', '៣', $text);
        $text = str_replace('4', '៤', $text);
        $text = str_replace('5', '៥', $text);
        $text = str_replace('6', '៦', $text);
        $text = str_replace('7', '៧', $text);
        $text = str_replace('8', '៨', $text);
        $text = str_replace('9', '៩', $text);
        $text = str_replace('0', '០', $text); 
        return $text;
        }
    
    
    add_filter('get_date', 'KhmerNumDate');
    add_filter('get_the_date', 'KhmerNumDate');
    add_filter('get_the_time', 'KhmerNumDate');
    
  2. Turns out it was because of the way the theme Twenty Twelve works (in their effort to make translation easy…).

    The date for posts in Twenty Twelve is prepared by a function in functions.php called twentytwelve_entry_meta()

    So to replace/translate the numbers in the date, I look for the line with $date= in the twentytwelve_entry_meta() function (or preferably replicate the twentytwelve_entry_meta() function in a child functions.php as I did):

    $date = sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></a>',
            esc_url( get_permalink() ),
            esc_attr( get_the_time() ),
            esc_attr( get_the_date( 'c' ) ),
            esc_html( get_the_date() )
    
        );
    
        //my added code - the_curlang() is used with the xili-language plugin, it returns the current language of the page
    
    $languagedate = the_curlang();
    
    if ( $languagedate == 'km_kh' ) {
    $date = str_replace('1', '១', $date);
    $date = str_replace('2', '២', $date);
    $date = str_replace('3', '៣', $date);
    $date = str_replace('4', '៤', $date);
    $date = str_replace('5', '៥', $date);
    $date = str_replace('6', '៦', $date);
    $date = str_replace('7', '៧', $date);
    $date = str_replace('8', '៨', $date);
    $date = str_replace('9', '៩', $date);
    $date = str_replace('0', '០', $date); 
    }
    

    Then everything looks great!

    I’m not much of a programmer, so my code might not be the best option, but at least it works.