php 如何在php中以2位数获取年份?

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

How to get the year in 2 digits in php?

php

提问by patricia perez

I want to get the year in 2 digits like 15.

我想以 2 位数字表示年份,例如15

using the php date() function

使用 php date() 函数

echo date("Y");

but this returns the full year.

但这会返回全年。

any help is much appriciated. Thanks!

任何帮助都非常有用。谢谢!

回答by Rick S

Change the upper case 'Y' to lower case 'y'

将大写“Y”更改为小写“y”

echo date("y");

PHP documentation

PHP 文档

y   A two digit representation of a year

回答by Prakash Bhandari

You can use substr() method of PHP to get the last two digit of a year as follows.

您可以使用 PHP 的 substr() 方法来获取年份的最后两位数字,如下所示。

$year =  2011;
$year = substr( $year, -2);

The above code gives 11 as a output which is the last two digit of 2011.

上面的代码给出了 11 作为输出,它是 2011 的最后两位数字。

回答by JeroenJK

http://php.net/manual/en/function.date.php

http://php.net/manual/en/function.date.php

See this documentation. It's date("y"). So a lowercase y instead of Y.

请参阅此文档。它是date("y")。所以小写的 y 而不是 Y。