7

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']);
9

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.

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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