PHP将短月份名称转换为月份号码

时间:2011-01-06 06:06:54

标签: php

  

可能重复:
  convert month from name to number

我有一个简单(但有趣)的查询。

我正在获得月份名称,简而言之,就像Jan,Feb等。现在我需要将其转换为月份数(即一个月的数字表示),带前导零 < / p>

示例:“Jan”到“01”,“Dec”到“12”等

任何人都知道,如何实现这一点,不使用数组

由于

7 个答案:

答案 0 :(得分:16)

$month = 'Feb';
echo date('m', strtotime($month));

strtotime将“2月”转换为 2011年2月6日15:15:00 的时间戳(在撰写本文时),从给定的字符串中获取所需内容并填写从当前时间的空白。 date('m')然后格式化此时间戳,仅输出月份数。

由于这可能会导致问题,例如31日,你应该填写这些空白以确定:

echo date('m', strtotime("$month 1 2011"));

答案 1 :(得分:2)

$date = strtotime ("Jan");
print date("m", $date);

有关strtotime的更多信息:PHP: strtotime - Manual

更多日期:PHP: date - Manual

答案 2 :(得分:2)

试试这个

$test  //whatever you getting
echo date("m",strtotime($test));

另见

http://php.net/manual/en/function.date.php

答案 3 :(得分:1)

strtotime会做到这一点。

前:

$monthName = 'Jan';

echo date('m', strtotime($monthName));

答案 4 :(得分:1)

鉴于你已经有了约会。

$date = 'Jun';
$month = date('m', strtotime($date));

答案 5 :(得分:1)

将它与日期和年份一起使用将保持良好状态:

$mon = 'Feb';
$month = date("F d Y",strtotime("$mon 31, 2010"));

它会正常工作。

答案 6 :(得分:-1)

这对我不起作用:

$mon = 'Aug';
$month = date("F",strtotime($mon));

我得到“十二月”???

然而,如果我把它放在一天和一年,它转换得很好:

$mon = 'Aug';
$month = date("F d Y",strtotime("$mon 31, 2011"));

它有效。

运行PHP版本5.1.2

相关问题