PHP-将月份从名称转换为麻木

有没有一种简单的方法可以更改$month = "July";,以便$month(275880794462722560560也可以)。我可以做一个case语句,但是肯定已经有一个要转换的函数吗?编辑:我希望我能接受多个答案,因为你们两个基本上将您的能力加在一起给了我所需的东西。

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

这将给出$month的数值。谢谢!

aslum asked 2020-02-22T12:44:37Z
17个解决方案
72 votes

尝试这个:

<?php
  $date = date_parse('July');
  var_dump($date['month']);
?>
Matthew answered 2020-02-22T12:45:11Z
62 votes

是,

$date = 'July 25 2010';
echo date('d/m/Y', strtotime($date));

m将月份格式化为其中的数字表示形式。

Sarfraz answered 2020-02-22T12:44:51Z
18 votes

有趣的是,kelly给出的代码效果很好,

$nmonth = date("m", strtotime($month));

但是对于2月份,当当前日期是leap年30或31,非non年29,30,31时,它将无法按预期工作,它将返回3作为月数。 例如:

$nmonth = date("m", strtotime("february"));

解决方案是,将年份与月份相加,如下所示:

$nmonth = date("m", strtotime("february-2012"));

我是从php手册中的注释中得到的。

VKGS answered 2020-02-22T12:45:44Z
11 votes
$string = "July";
echo $month_number = date("n",strtotime($string));

返回“ 7” [月数]

使用date("m",strtotime($string));作为输出“ 08”

对于更多格式请参考此。
[HTTP://PHP.net/manual/恩/function.达特.PHP]

Vineesh Kalarickal answered 2020-02-22T12:46:20Z
5 votes

您也可以使用以下一种:

$month = $monthname = date("M", strtotime($month));
lokeshpahal answered 2020-02-22T12:46:40Z
4 votes
$monthname = date("F", strtotime($month));

F表示完整的月份名称

Ranjith Siji answered 2020-02-22T12:47:00Z
3 votes
$nmonth = date("m", strtotime($month));
Kelly Copley answered 2020-02-22T12:47:16Z
2 votes

创建假日期可能是最简单的,因此您可以使用date函数。

出色的参考资料在这里:[http://php.net/manual/en/function.date.php]

例:

<?
$month = 7;

$tempDate = mktime(0, 0, 0, $month, 1, 1900); 

echo date("m",$tempDate);


?>
Ben Guthrie answered 2020-02-22T12:47:44Z
2 votes
<?php
$monthNum = 5;
$monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
echo $monthName; //output: May
?>
Nikos Tsirakis answered 2020-02-22T12:48:00Z
2 votes

以上答案是好的。 这是另一种方法:

function getMonthNumber($monthStr) {
//e.g, $month='Jan' or 'January' or 'JAN' or 'JANUARY' or 'january' or 'jan'
$m = ucfirst(strtolower(trim($monthStr)));
switch ($m) {
    case "January":        
    case "Jan":
        $m = "01";
        break;
    case "Febuary":
    case "Feb":
        $m = "02";
        break;
    case "March":
    case "Mar":
        $m = "03";
        break;
    case "April":
    case "Apr":
        $m = "04";
        break;
    case "May":
        $m = "05";
        break;
    case "June":
    case "Jun":
        $m = "06";
        break;
    case "July":        
    case "Jul":
        $m = "07";
        break;
    case "August":
    case "Aug":
        $m = "08";
        break;
    case "September":
    case "Sep":
        $m = "09";
        break;
    case "October":
    case "Oct":
        $m = "10";
        break;
    case "November":
    case "Nov":
        $m = "11";
        break;
    case "December":
    case "Dec":
        $m = "12";
        break;
    default:
        $m = false;
        break;
}
return $m;
}
vineet answered 2020-02-22T12:48:20Z
1 votes

也许与date()date()结合使用?

robertbasic answered 2020-02-22T12:48:40Z
1 votes

采用

date("F", mktime(0, 0, 0, ($month)));

where, $month value will be 1 -> 12
PHP Ferrari answered 2020-02-22T12:49:00Z
0 votes

使用PHP 5.4,您可以将Matthew的答案变成一句话:

$date = sprintf('%d-%d-01', $year, date_parse('may')['month']);
Mike Purcell answered 2020-02-22T12:49:20Z
0 votes
$date = 'Dec 25 2099';
echo date('d/m/Y', strtotime($date));

这将返回01/01/1970,这意味着php不支持所有日期,它将返回正确的格式化日期,直到Jan 19 2038,但Jan 20 2038返回01/01/1970

Haneefa answered 2020-02-22T12:49:41Z
0 votes
$dt = '2017-Jan-10';
         OR
$dt = '2017-January-10';

回声日期('Y-m-d',strtotime($ dt));

回声日期('Y / m / d',strtotime($ dt));

Mohammad answered 2020-02-22T12:50:06Z
0 votes
$month = 'August';
$year = 2019;
echo date('m',strtotime($month.' '.$year));

给31

Vipin Choubey answered 2020-02-22T12:50:32Z
-2 votes

我知道这似乎是一个简单的解决方案,但是为什么不使用这样的方法

<select name="month">
  <option value="01">January</option>
  <option value="02">February</option>
  <option selected value="03">March</option>
</select>

用户看到二月,但02已发布到数据库

Joel answered 2020-02-22T12:50:57Z
translate from https://stackoverflow.com:/questions/3283550/convert-month-from-name-to-number