doureng6738 2014-12-11 19:31
浏览 131

MLA风格月份缩写与PHP日期格式?

For those of who haven't take a lit class in a while, MLA is Modern Language Style and months are abbreviated like this

  • January - Jan.
  • February - Feb.
  • March - Mar.
  • April - Apr.
  • May - May
  • June - June
  • July - July
  • August - Aug.
  • September - Sept.
  • October - Oct.
  • November - Nov.
  • December - Dec.

With PHP, it's easy to get either abbreviated or not

$date = DateTime::createFromFormat('Ymd', $unformatted_date_string);
// abbreviated
echo $date->format('M d, Y');
// not abbreviated
echo $date->format('m d, Y');

But looked through http://php.net/manual/en/datetime.formats.date.php and not seeing a way to get a mix of both. Is there a better solution than string parsing?

  • 写回答

1条回答 默认 最新

  • douji6896 2016-11-21 16:46
    关注

    You could always just format months which are greater than 4 characters in length. It seems like that's all that you really want to do.

    // load array with all months for example
    for ($x = 1; $x <= 12; $x++) {
        $dates[] = new DateTime('2016-' . $x . '-1');
    }
    
    // length is > 4
    // if: echo abbreviated month
    // else: echo unformatted month
    foreach($dates as $date) {
        if (strlen($date->format('F')) > 4) {
            echo $date->format('M') . ".";
        } else {
            echo $date->format('F');
        }
    
        echo "
    ";
    }
    

    Results: Jan. Feb. Mar. Apr. May June July Aug. Sep. Oct. Nov. Dec.

    code on php fiddle

    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常