duanmianhong4893 2013-01-31 17:47
浏览 33
已采纳

strtotime翻译

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;
  • 写回答

1条回答 默认 最新

  • dongqu9917 2013-01-31 18:08
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?