解决方案
您可以使用 日期 或者 时间. 。在这种情况下,我想说这并不重要,因为无论如何,一年就是一年(除非有一个区域设置以不同的方式格式化年份?)
例如:
<?php echo date("Y"); ?>
顺便说一句,在 PHP 中格式化日期时,当您想要在与默认区域设置不同的区域设置中格式化日期时,这一点很重要。如果是这样,您必须使用 setlocale 和 strftime。根据 PHP手册 时间到了:
要使用其他语言格式化日期,您应该使用setLocale()和strftime()函数而不是date()。
从这个角度来看,我认为最好尽可能多地使用 strftime,即使您极有可能需要本地化您的应用程序。如果这不是问题,请选择您最喜欢的一个。
其他提示
<?php echo date("Y"); ?>
我显示版权行的超级懒惰版本,会自动保持更新:
© <?php
$copyYear = 2008;
$curYear = date('Y');
echo $copyYear . (($copyYear != $curYear) ? '-' . $curYear : '');
?> Me, Inc.
今年(2008年),它会说:
© 2008 Me, Inc. 版权所有
明年,它会说:
© 2008-2009 Me, Inc. 版权所有
并永远保持当前年份的更新。
或者(PHP 5.3.0+)使用匿名函数的紧凑方法,这样就不会泄漏变量并且不会重复代码/常量:
©
<?php call_user_func(function($y){$c=date('Y');echo $y.(($y!=$c)?'-'.$c:'');}, 2008); ?>
Me, Inc.
随着 PHP 朝着更加面向对象的方向发展,我很惊讶这里没有人引用内置的 DateTime
班级:
$now = new DateTime();
$year = $now->format("Y");
或在实例化时具有类成员访问权限的单行代码(php>=5.4):
$year = (new DateTime)->format("Y");
echo date('Y');
这给你当地时间:
$year = date('Y'); // 2008
和这个 世界标准时间:
$year = gmdate('Y'); // 2008
对于 4 位数字表示:
<?php echo date('Y'); ?>
2位数字表示:
<?php echo date('y'); ?>
检查 php 文档以获取更多信息:https://secure.php.net/manual/en/function.date.php
这就是我所做的:
<?php echo date("d-m-Y") ?>
下面是对其作用的一些解释:
d = day
m = month
Y = year
Y 将为您提供四位数字(例如1990)和 y 代表两位数(例如90)
echo date('Y')
为您提供当前年份,此后将自动更新 date()
给我们当前日期。
print date('Y');
有关更多信息,请查看 date() 函数文档: https://secure.php.net/manual/en/function.date.php
如果您的服务器支持短标签,或者您使用 PHP 5.4,则可以使用:
<?=date("Y")?>
写吧:
date("Y") // A full numeric representation of a year, 4 digits
// Examples: 1999 or 2003
或者:
date("y"); // A two digit representation of a year Examples: 99 or 03
并“回显”这个值......
使用刚刚调用的 PHP 函数 date()
.
它需要当前日期,然后您为其提供格式
格式就是 Y。大写 Y 将是四位数的年份。
<?php echo date("Y"); ?>
<?php echo date("Y"); ?>
这段代码应该做
您可以使用简单的 PHP 日期类。它提供了许多有用的方法和功能:
$date = new simpleDate();
echo $date->now()->getYear();
echo $date->now()->getMonth();
echo $date->set('2013-01-21')->getDayString();
echo $date->now()->compare('2013-01-21')->isBefore();
...
你可以检查 图书馆教程页面 更多示例
最高适用于 php 5.4+
<?php
$current= new \DateTime();
$future = new \DateTime('+ 1 years');
echo $current->format('Y');
//For 4 digit ('Y') for 2 digit ('y')
?>
或者你可以用一行来使用它
$year = (new DateTime)->format("Y");
如果你想增加或减少年份另一种方法;添加如下修改行。
<?PHP
$now = new DateTime;
$now->modify('-1 years'); //or +1 or +5 years
echo $now->format('Y');
//and here again For 4 digit ('Y') for 2 digit ('y')
?>
顺便提一句...有几种正确的方法来显示网站版权。有些人倾向于让事情变得多余,即:版权所有 © 两者含义相同。重要的版权部分是:
**Symbol, Year, Author/Owner and Rights statement.**
使用 PHP + HTML:
<p id='copyright'>© <?php echo date("Y"); ?> Company Name All Rights Reserved</p>
或者
<p id='copyright'>© <?php echo "2010-".date("Y"); ?> Company Name All Rights Reserved</p
<?php date_default_timezone_set("Asia/Kolkata");?><?=date("Y");?>
您可以在页脚部分使用它来获取动态版权年份
获取完整的使用年份:
<?php
echo $curr_year = date('Y'); // it will display full year ex. 2017
?>
或者只使用两位数的年份,如下所示:
<?php
echo $curr_year = date('y'); // it will display short 2 digit year ex. 17
?>
我的版权显示方式,不断自动更新
<p class="text-muted credit">Copyright ©
<?php
$copyYear = 2017; // Set your website start date
$curYear = date('Y'); // Keeps the second year updated
echo $copyYear . (($copyYear != $curYear) ? '-' . $curYear : '');
?>
</p>
它将输出结果为
copyright @ 2017 //if $copyYear is 2017
copyright @ 2017-201x //if $copyYear is not equal to Current Year.
使用 PHP date()
功能。
格式就是 Y。大写 Y 将是四位数的年份。
<?php echo date("Y"); ?>
本节的最佳简码:
<?= date("Y"); ?>
<?php
$time_now=mktime(date('h')+5,date('i')+30,date('s'));
$dateTime = date('d_m_Y h:i:s A',$time_now);
echo $dateTime;
?>