-1

I'm trying to translate the month name outputted by an ACF date field. How do I get the Spanish versions of those month names? Here's an example of my code:

$start_date = get_field('start_date'); $start_month = new DateTime($start_date); $start_month = $start_month->format('M'); // "Oct", "Nov", etc.

The main problem, I think, is that the date is saved as a date rather than a string... So it seems to me that using something like wpml_register_single_string() when the field is saved, is kind of pointless.

It seems like I should be telling the server to get the date in a different localization. I would envision the process to go something like this:

  1. WordPress retrieve's the ACF date from the database
  2. Based on the language set by the user (via WPML), we tell the server that we need the DateTime in that language/localization
  3. PHP outputs the date, translated appropriately

This seems logical to me, again, because we're not saving any strings to the database that should be translated. But, I'll be the first to admit, I don't have a clue what I'm doing here. Any help would be GREATLY appreciated!

3

You can use the global $wp_locale to get the month translated in each language. You just need to get the month number (01 to 12) from the Acf field.

You have the functions in the WP_Locale class get_month() and get_month_abbrev()

global $wp_locale;
$month = $wp_locale->get_month(04); // Output april in english
$month_abbrev = $wp_locale->get_month_abbrev($month); // Output Apr in english
0

After a bit more Googling, I finally found what I was looking for, using the WordPress function date_i18n().

It appears to read the language set by WPML and returns the ACF date in the correct language.

$end_date = get_field('end_date'); $end_month = date_i18n( 'M', strtotime( $end_date ) );

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.