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

时间:2008-09-15 15:34:55

标签: php date

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

31 个答案:

答案 0 :(得分:979)

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

例如:

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

另外,在PHP中格式化日期时,如果您希望使用与默认语言不同的语言环境格式化日期,则这很重要。如果是这样,您必须使用setlocale和strftime。根据日期php manual

  

要格式化其他语言的日期,   你应该使用setlocale()和   strftime()函数代替   日期()。

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

答案 1 :(得分:506)

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

答案 2 :(得分:201)

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

&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.

答案 3 :(得分:61)

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

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

或实例化时具有类成员访问权限的单行程序(php&gt; = 5.4):

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

答案 4 :(得分:29)

http://us2.php.net/date

echo date('Y');

答案 5 :(得分:27)

strftime("%Y");

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

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

答案 6 :(得分:14)

这个给你当地时间:

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

这一个 UTC

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

答案 7 :(得分:12)

对于4位数表示:

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

2位数表示:

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

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

答案 8 :(得分:11)

这就是我的所作所为:

<?php echo date("d-m-Y") ?>
下面是对它的作用的一些解释:

d = day
m = month
Y = year

Y将给出四位数(例如1990)和y给出两位数(例如90)

答案 9 :(得分:10)

echo date('Y')为您提供当前年份,并且会自动更新,因为date()会向我们提供当前日期。

答案 10 :(得分:9)

print date('Y');

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

答案 11 :(得分:7)

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

此代码应该

答案 12 :(得分:7)

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

<?=date("Y")?>

答案 13 :(得分: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

并且&#39;回声&#39;这个价值......

答案 14 :(得分:7)

使用一个名为date()的PHP函数。

它需要当前日期,然后您提供格式

格式就是Y.资本Y将是四位数。

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

答案 15 :(得分:5)

您可以使用简单的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();
...

您可以查看the library tutorials page了解更多示例

答案 16 :(得分:5)

最多支持php 5.4 +

for(int i = 0; i < 4000; i++){
    SDL_PumpEvents();
    SDL_Delay(1);
}

或者你可以用一行

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

答案 17 :(得分:4)

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

**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

答案 18 :(得分:2)

<?php date_default_timezone_set("Asia/Kolkata");?><?=date("Y");?>

您可以在页脚部分使用此功能来获取动态版权年度

答案 19 :(得分:2)

全年使用:

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

答案 20 :(得分:2)

我显示版权的方式,即自动更新

<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.

答案 21 :(得分:2)

使用PHP date()函数。

格式将为Y。大写字母Y将为四位数的年份。

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

答案 22 :(得分:1)

此部分的最佳简码:

<?= date("Y"); ?>

答案 23 :(得分:0)

如果您使用的是很棒的Carbon PHP API extension for DateTime,则可以轻松实现:

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

干杯。

答案 24 :(得分:0)

$year = date("Y", strtotime($yourDateVar));

答案 25 :(得分:0)

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

想法很简单,但涉及的步骤甚至超过了预期。

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

  2. 找到版权文本,希望将其全部硬编码但找到:

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

    enter image description here,然后在下一页上转到标签Disclaimers

    enter image description here,然后在顶部附近找到版权年份:

    enter image description here删除©符号+年+年后的空白,然后使用页面右上方的Update按钮保存页面。

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

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

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

答案 26 :(得分:0)

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

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

答案 27 :(得分:0)

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

//获取当前年份使用 //PHP的日期函数。

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

//获取当前年份使用 //PHP的日期函数。

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

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

如果你只想检索2位格式,那么你可以使用小写的“y”格式字符:

$year = date("y"); 回声$年; 1 2 $year = date("y"); 回声$年; 上面的代码段将打印 20 而不是 2020,或 19 而不是 2019,等等。

答案 28 :(得分:0)

在 Laravel 中

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

在 PHP 中

echo date("Y");

答案 29 :(得分:-1)

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

答案 30 :(得分:-1)

日期函数中第二个参数的更多价格 strtotime 返回 param 传递的时间戳

// 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');
相关问题