【问题标题】:date("m"); returns the following result for this month... "02"日期(“m”);返回本月的以下结果...“02”
【发布时间】:2011-02-17 20:35:56
【问题描述】:

日期(“m”);返回本月的以下结果...“02”

这很好,花花公子,但问题是,我需要将 02 更改为二月,而不是将其更改为 date("F");

我需要使用 date 函数将提供的数字 02 转换为二月,有没有办法在没有一堆 if/else 语句的情况下从逻辑上解决这个问题??

【问题讨论】:

  • 有什么理由不能使用 date('F').我知道你说你不能,但是不改变 m->F 的原因是什么?
  • @Marc - 你完全没有抓住重点。他想将“02”转换为“二月”
  • F 是唯一可能使用的格式说明符,如果您想“使用日期函数”将表示二月的时间戳转换为名称“二月”。

标签: php function date


【解决方案1】:

虽然首选日期('F'):

$monthnames = array(
  '01'=>'January',
  '02'=>'February',
  '03'=>'March'
); // and so on...

echo $monthnames[date("m")];

编辑 调整到迄今未指定的要求:

$ts = mktime (0, 0, 0, date("m"), 1, date("Y"));

文档:http://us2.php.net/manual/en/function.mktime.php

【讨论】:

  • 你说你不能使用 date('F')。我不明白你的评论是什么意思。
  • 我得到像 01,02...12 这样的月份编号,我想使用日期函数显示月份全名。
  • @Layric:使用date() 的唯一方法是使用date('F', $timestamp)。如果你做不到,你就做不到。
【解决方案2】:
<?php
$convertIntegerToMonth = array(
    1 => 'January',
    2 => 'February',
    3 => 'March',
    ...
);

$number = 2;
echo $convertIntegerToMonth[(int)$number];
?>

【讨论】:

  • Erp.要么我错过了 int 的演员阵容,要么你是忍者编辑的。 :)
【解决方案3】:

忘记那些数组!本地化呢?

这会将“02”转换为“二月”

<?php
$month = date("m");    // "02"
$newstr = date("F", mktime(0, 0, 0, $month));   // "February"
?>

http://us2.php.net/manual/en/function.mktime.php

【讨论】:

    【解决方案4】:

    一个简单的方法:

    date('F', strtotime("2000-$month-01"))

    【讨论】:

      【解决方案5】:

      这段代码运行正常:

      <?php 
          $year = 2016;
          $month = 02;
          echo date("M", mktime(null, null, null,$month+1, null, $year));
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-12
        • 2020-01-30
        • 2013-02-19
        相关资源
        最近更新 更多