如何使用 PHP 获取当前年份?

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

How do I use PHP to get the current year?

phpdate

提问by JD Graffam

I want to put a copyright notice in the footer of a web site, but I think it's incredibly tacky for the year to be out-of-date. How would I make the year update automatically with PHP 4and PHP 5?

我想在网站的页脚中放置版权声明,但我认为过时的年份非常俗气。我如何使用PHP 4PHP 5自动更新年份?

回答by Erik van Brakel

You can use either dateor strftime. In this case I'd say it doesn't matter as a year is a year, no matter what (unless there's a locale that formats the year differently?)

您可以使用datestrftime。在这种情况下,我会说这并不重要,因为一年是一年,无论如何(除非有一个以不同方式格式化年份的语言环境?)

For example:

例如:

<?php echo date("Y"); ?>

On a side note, when formatting dates in PHP it matters when you want to format your date in a different locale than your default. If so, you have to use setlocale and strftime. According to the php manualon date:

附带说明一下,在 PHP 中格式化日期时,当您想在与默认设置不同的语言环境中格式化日期时很重要。如果是这样,您必须使用 setlocale 和 strftime。根据日期的php手册

To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().

要格式化其他语言的日期,您应该使用 setlocale() 和 strftime() 函数而不是 date()。

From this point of view, I think it would be best to use strftime as much as possible, if you even have a remote possibility of having to localize your application. If that's not an issue, pick the one you like best.

从这个角度来看,我认为最好尽可能使用 strftime,如果您甚至有可能不得不本地化您的应用程序。如果这不是问题,请选择您最喜欢的那个。

回答by Daniel Papasian

<?php echo date("Y"); ?>

回答by gregmac

My super lazy version of showing a copyright line, that automatically stays updated:

我显示版权行的超级懒惰版本,它会自动保持更新:

&copy; <?php 
$copyYear = 2008; 
$curYear = date('Y'); 
echo $copyYear . (($copyYear != $curYear) ? '-' . $curYear : '');
?> Me, Inc.

This year (2008), it will say:

今年(2008),它会说:

© 2008 Me, Inc.

© 2008 Me, Inc.

Next year, it will say:

明年,它会说:

© 2008-2009 Me, Inc.

© 2008-2009 Me, Inc.

and forever stay updated with the current year.

并永远保持与当前年份的更新。



Or (PHP 5.3.0+) a compact way to do it using an anonymous function so you don't have variables leaking out and don't repeat code/constants:

或者 (PHP 5.3.0+) 一种使用匿名函数的紧凑方法,这样您就不会泄漏变量,也不会重复代码/常量:

&copy; 
<?php call_user_func(function($y){$c=date('Y');echo $y.(($y!=$c)?'-'.$c:'');}, 2008); ?> 
Me, Inc.

回答by Thomas Kelley

With PHP heading in a more object-oriented direction, I'm surprised nobody here has referenced the built-in DateTimeclass:

随着 PHP 朝着更加面向对象的方向发展,我很惊讶这里没有人引用内置DateTime类:

$now = new DateTime();
$year = $now->format("Y");

or one-liner with class member access on instantiation (php>=5.4):

或在实例化时具有类成员访问权限的单行 (php>=5.4):

$year = (new DateTime)->format("Y");

回答by chrisb

回答by Mark Biek

strftime("%Y");

I love strftime. It's a great function for grabbing/recombining chunks of dates/times.

我爱strftime。这是抓取/重新组合日期/时间块的一个很好的功能。

Plus it respects locale settings which the date function doesn't do.

另外它尊重日期功能不做的区域设置。

回答by Alexey Lebedev

This one gives you the local time:

这个给你当地时间:

$year = date('Y'); // 2008

And this one UTC:

而这个UTC

$year = gmdate('Y'); // 2008

回答by Abdul Rahman A Samad

Here's what I do:

这是我所做的:

<?php echo date("d-m-Y") ?>

below is a bit of explanation of what it does:

下面是对它的作用的一些解释:

d = day
m = month
Y = year

Y will gives you four digit (e.g. 1990) and y for two digit (e.g. 90)

Y 将为您提供四位数(例如 1990)和 y 为两位数(例如 90)

回答by joan16v

For 4 digit representation:

对于 4 位数字表示:

<?php echo date('Y'); ?>

2 digit representation:

2位数表示:

<?php echo date('y'); ?>

Check the php documentation for more info: https://secure.php.net/manual/en/function.date.php

查看 php 文档了解更多信息:https: //secure.php.net/manual/en/function.date.php

回答by kkarayat

echo date('Y')gives you current year, and this will update automatically since date()give us the current date.

echo date('Y')给你当前年份,这将自动更新,因为date()给我们当前日期。