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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4612246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:41:15  来源:igfitidea点击:

PHP convert short month-name to month-number

php

提问by I-M-JM

Possible Duplicate:
convert month from name to number

可能的重复:
将月份从名称转换为数字

I have a simple (yet interesting) query for all.

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

I am getting month name, in short, like Jan, Feb, etc. Now I need to convert it to month-number ( i.e., Numeric representation of a month), with leading zeros

我得到月份名称,简而言之,如 Jan、Feb 等。现在我需要将其转换为月份数字(即月份的数字表示),并带有前导零

Example: "Jan" to "01", "Dec" to "12", etc

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

Anyone know, how to achieve this, without using array

任何人都知道,如何在不使用数组的情况下实现这一目标

Thanks

谢谢

回答by deceze

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

strtotimeconverts "Feb" to the timestamp for February 6th 2011 15:15:00(at the time of writing), taking what it can from the given string and filling in the blanks from the current time. date('m')then formats this timestamp, outputting only the month number.

strtotime将 "Feb" 转换为2011 年 2 月 6 日 15:15:00(撰写本文时)的时间戳,从给定的字符串中获取它可以获取的内容并从当前时间开始填充空白。date('m')然后格式化这个时间戳,只输出月份数字。

Since this may actually cause problems on, say, the 31st, you should fill in these blanks to be sure:

由于这实际上可能会导致问题,比如说,31 日,您应该填写这些空白以确保:

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

回答by Anush Prem

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

More on strtotime: PHP: strtotime - Manual

更多关于 strtotime:PHP:strtotime - 手册

More on date: PHP: date - Manual

更多关于日期:PHP:日期 - 手册

回答by Bhanu Prakash Pandey

try this

尝试这个

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

also see

另见

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

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

回答by John

use this with a day and year it will covert fine:

将它与一天和一年一起使用它会很好地隐藏:

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

it will work fine.

它会正常工作。

回答by Romelus

Given that you already have your date.

鉴于你已经有了约会对象。

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

回答by seanh

strtotime will do the trick.

strtotime 会解决问题。

ex:

前任:

$monthName = 'Jan';

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

回答by Paul

This does not work for me:

这对我不起作用:

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

I get "December" ???

我得到“十二月”???

however, if I place this with a day and year it converts fine:

但是,如果我把它放在一天和一年里,它会很好地转换:

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

it works.

有用。

running PHP Version 5.1.2

运行 PHP 版本 5.1.2