PHP:日期函数获取当前日期的月份

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

PHP: date function to get month of the current date

phpdatetime

提问by iamjonesy

I want to be able to figure out the month of the current date variable. I'm ex vb.net and the way to do it there is just date.Month. How do I do this in PHP?

我希望能够找出当前日期变量的月份。我是 vb.net 的前任,这样做的方法只有date.Month. 我如何在 PHP 中做到这一点?

Thanks,

谢谢,

Jonesy

琼斯

I used date_format($date, "m"); //01, 02..12

我用了 date_format($date, "m"); //01, 02..12

This is what I wanted, question now is how do I compare this to an int since $monthnumber = 01just becomes 1

这就是我想要的,现在的问题是我如何将它与 int 进行比较,因为它$monthnumber = 01刚刚变成1

回答by fabrik

See http://php.net/date

http://php.net/date

date('m')or date('n')or date('F')...

date('m')date('n')date('F')...

Update

更新

mNumeric representation of a month, with leading zeros 01 through 12

nNumeric representation of a month, without leading zeros 1 through 12

FAlphabetic representation of a month January through December

m月份的数字表示,前导零为 01 到 12

n月份的数字表示,没有前导零 1 到 12

F一月到十二月的字母表示

....see the docs link for even more options.

....查看文档链接以获取更多选项。

回答by oezi

What does your "data variable" look like? If it's like this:

你的“数据变量”是什么样的?如果是这样:

$mydate = "2010-05-12 13:57:01";

You can simply do:

你可以简单地做:

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

For more information, take a look at dateand strtotime.

有关更多信息,请查看datestrtotime

EDIT:

编辑:

To compare with an int, just do a date_format($date,"n");which will give you the month without leading zero.

要与 int 进行比较,只需执行 a 即可date_format($date,"n");为您提供没有前导零的月份。

Alternatively, try one of these:

或者,尝试以下方法之一:

if((int)$month == 1)...
if(abs($month) == 1)...

Or something weird using ltrim, round, floor... but date_format() with "n" would be the best.

或者使用 ltrim、round、floor 进行一些奇怪的操作……但是带有“n”的 date_format() 会是最好的。

回答by Pramendra Gupta

$unixtime = strtotime($test);
echo date('m', $unixtime); //month
echo date('d', $unixtime); 
echo date('y', $unixtime );

回答by Hannes

as date_format uses the same format as date ( http://www.php.net/manual/en/function.date.php) the "Numeric representation of a month, without leading zeros" is a lowercase n .. so

由于 date_format 使用与日期相同的格式(http://www.php.net/manual/en/function.date.php)“月份的数字表示,没有前导零”是小写 n .. 所以

echo date('n'); // "9"

回答by J-a-n-u-s

As it's not specified if you mean the system's current date or the date held in a variable, I'll answer for latter with an example.

由于它没有指定是系统的当前日期还是变量中保存的日期,我将用一个例子来回答后者。

<?php
$dateAsString = "Wed, 11 Apr 2018 19:00:00 -0500";

// This converts it to a unix timestamp so that the date() function can work with it.
$dateAsUnixTimestamp = strtotime($dateAsString);

// Output it month is various formats according to http://php.net/date

echo date('M',$dateAsUnixTimestamp);
// Will output Apr

echo date('n',$dateAsUnixTimestamp);
// Will output 4

echo date('m',$dateAsUnixTimestamp);
// Will output 04
?>

回答by e102

To compare with an int do this:

要与 int 进行比较,请执行以下操作:

<?php
$date = date("m");
$dateToCompareTo = 05;
if (strval($date) == strval($dateToCompareTo)) {
    echo "They are the same";
}
?>