php 将月份从名称转换为数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3283550/
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 09:13:57  来源:igfitidea点击:

convert month from name to number

phptime

提问by aslum

Is there an easy way to change $month = "July";so that $nmonth = 7(07would be fine too). I could do a case statement, but surely there is already a function to convert? EDIT: I wish I could accept multiple answers, cause two of you basically gave me what I needed by your powers combined.

有一个简单的方法来改变$month = "July";,这样$nmonth = 707就可以了太)。我可以做一个 case 语句,但肯定已经有一个函数可以转换?编辑:我希望我能接受多个答案,因为你们两个基本上通过你们的力量给了我我需要的东西。

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

That will give the numerical value for $month. Thanks!

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

回答by Matthew

Try this:

尝试这个:

<?php
  $date = date_parse('July');
  var_dump($date['month']);
?>

回答by Sarfraz

Yes,

是的,

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

The mformats the month to its numerical representation there.

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

回答by vkGunasekaran

An interesting look here, the code given by kelly works well,

一个有趣的样子,kelly 给出的代码运行良好,

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

but for the month of february, it won't work as expected when the current day is 30 or 31 on leap year and 29,30,31 on non-leap year.It will return 3as month number. Ex:

但是对于二月,当闰年的当前日期为 30 或 31 以及非闰年的 29,30,31 时,它不会按预期工作。它将返回3作为月份数。前任:

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

The solution is, add the year with the month like this:

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

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

I got this from this commentin php manual.

我从php 手册中的这个评论中得到了这个。

回答by Vineesh Kalarickal

$string = "July";
echo $month_number = date("n",strtotime($string));

returns '7' [month number]

返回 '​​7' [月数]

Use date("m",strtotime($string));for the output "08"

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

For more formats reffer this..
http://php.net/manual/en/function.date.php

有关更多格式,请参阅此 ..
http://php.net/manual/en/function.date.php

回答by lokeshpahal

you can also use this one:

你也可以使用这个:

$month = $monthname = date("M", strtotime($month));

回答by Ranjith Siji

$monthname = date("F", strtotime($month));

Fmeans full month name

F表示完整的月份名称

回答by Kelly Copley

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

回答by Ben Guthrie

It may be easiest to create a fake date so you can use the date function.

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

Excellent reference here: http://php.net/manual/en/function.date.php

这里的优秀参考:http: //php.net/manual/en/function.date.php

Example:

例子:

<?
$month = 7;

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

echo date("m",$tempDate);


?>

回答by Nikos Tsirakis

<?php
$monthNum = 5;
$monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
echo $monthName; //output: May
?>

回答by PHP Ferrari

Use

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

where, $month value will be 1 -> 12