在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:06:21  来源:igfitidea点击:

Simplest way to display current month and year like "Aug 2016" in PHP?

phpdatedatetimeecho

提问by Sam

What is the shortest, simplest code to generate the curent month in Full English like Septemberor in abbreviated three letter version like Feband then add the current Year 2011?

用完整的英文September或缩写的三个字母版本生成当前月份Feb然后添加当前年份的最短,最简单的代码是什么2011

So the code will, depending on the month and year, echo things like:

因此,代码将根据月份和年份回显如下内容:

August 2016or Aug 2016etcettera. Thanks!

August 2016Aug 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 timestampparameter for the date() function. Like this:

为了显示上个月,我们必须引入mktime()函数并使用timestampdate() 函数的可选参数。像这样:

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');