开发者

PHP convert short month-name to month-number [duplicate]

This question already has answers here: Closed 10 years ago.

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

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

Anyone know, how开发者_开发技巧 to achieve this, without using array

Thanks


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

strtotime converts "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.

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

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


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

More on strtotime: PHP: strtotime - Manual

More on date: PHP: date - Manual


try this

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

also see

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


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.


strtotime will do the trick.

ex:

$monthName = 'Jan';

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


Given that you already have your date.

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜