在 PHP 中显示当前月份和年份的最简单方法,如“2016 年 8 月”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5347217/
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
Simplest way to display current month and year like "Aug 2016" in PHP?
提问by Sam
What is the shortest, simplest code to generate the curent month in Full English like September
or in abbreviated three letter version like Feb
and then add the current Year 2011
?
用完整的英文September
或缩写的三个字母版本生成当前月份Feb
然后添加当前年份的最短,最简单的代码是什么2011
?
So the code will, depending on the month and year, echo things like:
因此,代码将根据月份和年份回显如下内容:
August 2016
or Aug 2016
etcettera. Thanks!
August 2016
或 Aug 2016
等等。谢谢!
回答by RDL
Full version:
完整版本:
<? echo date('F Y'); ?>
Short version:
精简版:
<? echo date('M Y'); ?>
Here is a good reference for the different date options.
update
更新
To show the previous month we would have to introduce the mktime()function and make use of the optional timestamp
parameter for the date() function. Like this:
为了显示上个月,我们必须引入mktime()函数并使用timestamp
date() 函数的可选参数。像这样:
echo date('F Y', mktime(0, 0, 0, date('m')-1, 1, date('Y')));
This will also work (it's typically used to get the last day of the previous month):
这也可以(通常用于获取上个月的最后一天):
echo date('F Y', mktime(0, 0, 0, date('m'), 0, date('Y')));
Hope that helps.
希望有帮助。
回答by shacharsol
Here is a simple and more update format of getting the data:
这是获取数据的简单且更更新的格式:
$now = new \DateTime('now');
$month = $now->format('m');
$year = $now->format('Y');