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']);
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0