我想在一个网站的页脚放一个版权声明,但我认为这对于过时的年份来说是非常俗气的。
如何在php4或php5中自动更新年份?
我想在一个网站的页脚放一个版权声明,但我认为这对于过时的年份来说是非常俗气的。
如何在php4或php5中自动更新年份?
当前回答
顺便说一句……网站版权展示有几种合适的方式。有些人倾向于让事情变得多余,例如:版权©具有相同的含义。重要的版权部分包括:
**Symbol, Year, Author/Owner and Rights statement.**
使用PHP + HTML:
<p id='copyright'>© <?php echo date("Y"); ?> Company Name All Rights Reserved</p>
or
<p id='copyright'>© <?php echo "2010-".date("Y"); ?> Company Name All Rights Reserved</p
其他回答
适用于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')
?>
我显示版权的方式,它会自动更新
<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.
print date('Y');
有关更多信息,请查看date()函数文档:https://secure.php.net/manual/en/function.date.php
我的超级懒惰版本显示版权行,自动保持更新:
© <?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.
在Laravel
$date = Carbon::now()->format('Y');
return $date;
在PHP中
echo date("Y");