Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have date format and would like to translate to another language with date_i18n function how can I integrate with get_post_time here is my code :

$time = get_post_time('F j, Y', true,$newspost['ID']);
share|improve this question
add comment

1 Answer

up vote 9 down vote accepted

Use the fourth parameter for get_post_time():

$time = get_post_time(
    'F j, Y',      // format
    TRUE,          // GMT
    get_the_ID(),  // Post ID
    TRUE           // translate, use date_i18n()
);

get_post_time() calls mysql2date() internally, and it passes the $translate argument through. In mysql2date() we find this:

if ( $translate )
    return date_i18n( $format, $i );

So, all you need is a single TRUE.

For a test, try this:

add_filter( 'the_content', 'wpse_100266_i18n_time' );

/**
 * Prepend the post content with translated post time.
 *
 * @wp-hook the_content
 * @param   string $content
 * @return  string
 */
function wpse_100266_i18n_time( $content )
{
    $time = get_post_time(
        'F j, Y',      // format
        TRUE,          // GMT
        get_the_ID(),  // Post ID
        TRUE           // translate, use date_i18n()
    );

    return "<p>$time</p>$content";
}

Then install at least one other language and the plugin WCM User Language Switcher. Viewing the front end, we get different month names when we switch the language now.

get_post_modified_time() works with the same arguments.

share|improve this answer
    
Hi, it works nice.. " $time = get_post_time('j M Y', true,$newspost->ID,true); " Thank you very much 5 stars –  Decneo May 22 '13 at 10:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.