Question

Working on a Wordpress theme that I had to convert the locale to french.

    $data_event     = get_post_meta($post->ID, 'event_date_interval', true);
    $time           = strtotime($data_event);
    $pretty_date_yy = date('Y', $time);
    setlocale (LC_ALL, "fr_FR");
    $translate_fr  = strftime("%h", strtotime($data_event));
    $pretty_date_M = htmlentities($translate_fr);
    $pretty_date_d  = date('d', $time);

This works fine, it shows everything as it should (For example, right now we are in February so it shows FÉVR)

However my problem lies in that I want it to show FÉV. and not FÉVR. Is it possible to change this abbreviation?

EDIT: Solution was to create an array and set the specific names I wanted. It was not encoding properly utf8_encode was added. Thanks Phex!

    $data_event     = get_post_meta($post->ID, 'event_date_interval', true);
    $time           = strtotime($data_event);
    setlocale (LC_ALL, "fr_FR");
    $pretty_date_yy = date('Y', $time);
    $pretty_date_d  = date('d', $time);
    $id = intval(strftime("%m", strtotime($data_event))) - 1;
    $abr_map = array(
      'Jan',
      'Fév',
      'Mar',
      'Avr',
      'Mai',
      'Juin',
      'Juil',
      'Aout',
      'Sept',
      'Oct',
      'Nov',
      'Déc'
    );
    $translate_fr = htmlentities(utf8_decode($abr_map[$id]));
    $pretty_date_M = $translate_fr;
Was it helpful?

Solution

One way to do it would be to use PHPs substr function as follows:

$translate_fr  = substr(strftime("%h", strtotime($data_event)), 0, 3);

Edit: In case not all months should be abbreviated to three characters, it would be possible to use an associative array as a map:

$abr_map = array(
  'JANV' => 'Jan',
  'FÉVR' => 'Fév',
  'MARS' => 'Mar',
  'AVRI' => 'Avr',
  'MAI'  => 'Mai',
  'JUIN' => 'Juin',
  'JUIL' => 'Juil',
  'AOUT' => 'Aout',
  'SEPT' => 'Sept',
  'OCTO' => 'Oct',
  'NOVE' => 'Nov',
  'DÉCE' => 'Déc'
);

Alternatively using intval and strftimes %m formatter to provide an integer "key" for an indexed array:

$id = intval(strftime("%m", strtotime($data_event))) - 1;

$abr_map = array(
  'Jan',
  'Fév',
  'Mar',
  'Avr',
  'Mai',
  'Juin',
  'Juil',
  'Aout',
  'Sept',
  'Oct',
  'Nov',
  'Déc'
);

To use the map within the function, you would then use

$translate_fr = htmlentities(utf8_decode($abr_map[$id]));

or alternatively using htmlentities built in encoder:

$translate_fr = htmlentities($abr_map[$id], ENT_COMPAT, 'UTF-8');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top