我想在一个网站的页脚放一个版权声明,但我认为这对于过时的年份来说是非常俗气的。

如何在php4或php5中自动更新年份?


当前回答

在Laravel

$date = Carbon::now()->format('Y');
return $date;

在PHP中

echo date("Y");

其他回答

在date函数的第二个参数中可以得到更多的值 Strtotime返回参数传递的时间戳

// This work when you get time as string
echo date('Y', strtotime("now"));

// Get next years
echo date('Y', strtotime("+1 years"));

// 
echo strftime("%Y", strtotime("now"));

使用datetime类

echo (new DateTime)->format('Y');

可以使用date或strftime。在这种情况下,我会说这并不重要,因为一年是一年,无论如何(除非有一个地区格式的年不同?)

例如:

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

另一方面,在PHP中格式化日期时,如果您希望使用不同于默认语言环境的语言环境格式化日期,则很重要。如果是,你必须使用setlocale和strftime。根据php手册日期:

要设置其他语言的日期格式, 您应该使用setlocale()和 Strftime()函数代替 日期()。

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

<?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;
?>

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

&copy; <?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+)一种使用匿名函数的紧凑方法,这样你就不会有变量泄露,也不会重复代码/常量:

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

只写:

date("Y") // A full numeric representation of a year, 4 digits
          // Examples: 1999 or 2003

Or:

date("y"); // A two digit representation of a year     Examples: 99 or 03

然后“echo”这个值…