1067

我想在网站的页脚添加版权声明,但我认为过时的年份太俗气了。

如何使用PHP 4PHP 5自动更新年份?

4

32 回答 32

1332

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

例如:

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

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

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

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

于 2008-09-15T15:45:50.990 回答
526
<?php echo date("Y"); ?>
于 2008-09-15T15:35:48.110 回答
214

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

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

今年(2008 年),它会说:

© 2008 我公司。

明年,它会说:

© 2008-2009 我公司。

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


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

&copy; 
<?php call_user_func(function($y){$c=date('Y');echo $y.(($y!=$c)?'-'.$c:'');}, 2008); ?> 
Me, Inc.
于 2008-09-15T22:51:11.357 回答
78

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

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

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

$year = (new DateTime)->format("Y");
于 2013-01-02T18:03:06.773 回答
31

http://us2.php.net/date

echo date('Y');
于 2008-09-15T15:36:36.767 回答
30
strftime("%Y");

我喜欢strftime。这是抓取/重新组合日期/时间块的绝佳功能。

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

于 2008-09-15T15:35:53.213 回答
16

这个给你当地时间:

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

而这个UTC

$year = gmdate('Y'); // 2008
于 2008-09-15T15:44:58.557 回答
14

这就是我所做的:

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

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

d = day
m = month
Y = year

Y 会给你四位数字(例如 1990)和 y 两位数(例如 90)

于 2014-01-17T15:52:12.267 回答
12

对于 4 位表示:

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

2位数字表示:

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

查看 php 文档以获取更多信息: https ://secure.php.net/manual/en/function.date.php

于 2014-10-16T08:02:56.580 回答
10

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

于 2014-04-11T07:08:02.777 回答
9
print date('Y');

有关更多信息,请查看 date() 函数文档:https ://secure.php.net/manual/en/function.date.php

于 2008-09-15T15:37:52.677 回答
8

使用刚刚调用的 PHP 函数date()

它采用当前日期,然后您为其提供格式

格式将是 Y。大写 Y 将是一个四位数的年份。

<?php echo date("Y"); ?>
于 2017-02-09T20:19:00.090 回答
8
<?php echo date("Y"); ?>

这段代码应该做

于 2017-02-15T23:22:33.350 回答
7

写吧:

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

并“回显”这个值......

于 2016-10-26T07:02:16.650 回答
7

使用 PHPdate()函数。

格式将是 Y。大写 Y 将是一个四位数的年份。

<?php echo date("Y"); ?>
于 2018-12-28T09:45:00.893 回答
6

如果您的服务器支持短标签,或者您使用 PHP 5.4,您可以使用:

<?=date("Y")?>
于 2012-08-30T16:32:12.173 回答
6

顺便说一句...有一些正确的方法如何显示网站版权。有些人倾向于使事情变得多余,即:版权©具有相同的含义。重要的版权部分是:

**Symbol, Year, Author/Owner and Rights statement.** 

使用 PHP + HTML:

<p id='copyright'>&copy; <?php echo date("Y"); ?> Company Name All Rights Reserved</p>

或者

<p id='copyright'>&copy; <?php echo "2010-".date("Y"); ?> Company Name All Rights Reserved</p
于 2016-11-28T02:07:02.823 回答
6

最高 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')
?>
于 2016-12-25T00:57:20.037 回答
5

获得全年使用:

 <?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
?>
于 2017-09-15T05:07:10.010 回答
3

我展示版权的方式,不断自动更新

<p class="text-muted credit">Copyright &copy;
    <?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.
于 2017-11-10T05:55:56.467 回答
3

本节的最佳简码:

<?= date("Y"); ?>
于 2019-03-15T16:48:53.310 回答
3

要使用 PHP 的日期函数获取当前年份,您可以传入“Y”格式字符,如下所示:

//Getting the current year using
//PHP's date function.

$year = date("Y");
echo $year;

上面的示例将打印出当前年份的完整 4 位表示。

如果您只想检索 2 位格式,则可以使用小写“y”格式字符:

$year = date("y");
echo $year;
1
2
$year = date("y");
echo $year;

上面的代码段将打印出 20 而不是 2020,或 19 而不是 2019,等等。

于 2021-02-01T08:31:44.590 回答
2
<?php date_default_timezone_set("Asia/Kolkata");?><?=date("Y");?>

您可以在页脚部分使用它来获得动态版权年份

于 2017-09-09T05:51:49.033 回答
1
$year = date("Y", strtotime($yourDateVar));
于 2020-04-01T13:39:07.933 回答
1

在 Laravel 中

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

在 PHP 中

echo date("Y");
于 2021-06-16T09:50:03.387 回答
0

就我而言,wordpress 网站页脚中的版权声明需要更新。

以为简单,但涉及的步骤或超出预期。

  1. footer.php在您的主题文件夹中打开。

  2. 找到版权文本,预计这都是硬编码的,但发现:

    <div id="copyright">
        <?php the_field('copyright_disclaimer', 'options'); ?>
    </div>
    
  3. 现在我们知道年份写在 WordPress 管理员的某个地方,所以找到它以删除年份文字。在 WP-Admin 中,转到Options左侧的主管理菜单:

    在此处输入图像描述然后在下一页转到选项卡Disclaimers

    在此处输入图像描述在顶部附近,您会发现版权年份:

    在此处输入图像描述删除©符号+年份+年份后面的空白区域,然后使用页面Update右上角的按钮保存页面。

  4. 现在删除年份的文本版本,我们可以去添加使用 PHP 自动更新的年份。回到第 2步中找到的代码块footer.php并将其更新为:

    <div id="copyright">
        &copy;<?php echo date("Y"); ?> <?php the_field('copyright_disclaimer', 'options'); ?>
    </div>
    
  5. 完毕!只需要测试以确保更改已按预期生效。

对于许多人来说,情况可能并不相同,但是我们在很多客户站点中都遇到了这种模式,并认为最好在此处记录。

于 2020-11-20T21:11:33.523 回答
0

用 M 打印当前月份,用 D 打印日期,用 Y 打印年份。

<?php echo date("M D Y"); ?>
于 2021-01-28T06:28:24.387 回答
0

有关日期函数 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"));

有日期时间类

echo (new DateTime)->format('Y');
于 2021-03-02T12:31:03.703 回答
0

创建一个辅助函数并调用它

getCurrentYear();

function getCurrentYear(){
    return now()->year;
}
于 2021-09-08T13:34:32.330 回答
0
$dateYear = date('Y');
echo "Current Year: $dateYear";

当年:2022

$dateYear = date('y');
echo $dateYear;

22

于 2022-01-03T16:50:07.810 回答
-2
<?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;
?>
于 2014-10-02T10:18:54.667 回答
-3

如果您使用的是用于 DateTime 的 Carbon PHP API 扩展,则可以轻松实现:

<?php echo Carbon::now()->year; ?>

于 2019-06-11T22:40:49.250 回答