简体   繁体   English

strtotime翻译

[英]strtotime Translations

Working on a Wordpress theme that I had to convert the locale to french. 使用Wordpress主题,我必须将语言环境转换为法语。

    $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) 这样可以正常工作,它可以显示应有的一切(例如,现在我们在二月,所以它显示了FÉVR)

However my problem lies in that I want it to show FÉV. 不过我的问题在于我希望它能展示FÉV。 and not FÉVR. 而不是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. 它没有正确编码utf8_encode被添加。 Thanks Phex! 谢谢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;

One way to do it would be to use PHPs substr function as follows: 一种方法是使用PHPs substr函数,如下所示:

$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 strftime s %m formatter to provide an integer "key" for an indexed array: 或者使用intvalstrftime s %m格式化程序为索引数组提供整数“键”:

$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: 或者使用编码器内置的htmlentities

$translate_fr = htmlentities($abr_map[$id], ENT_COMPAT, 'UTF-8');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM