首页 文章

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

提问于
浏览
772

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

22 回答

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

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

  • 4

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

    <?=date("Y")?>
    
  • 6

    这是我做的:

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

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

    d = day
    m = month
    Y = year
    

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

  • 29
    print date('Y');
    

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

  • 12

    您可以使用datestrftime . 在这种情况下,我认为一年是一年,不管是什么(除非有一个区域设置以不同的方式格式化年份?)

    例如:

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

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

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

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

  • 9
    strftime("%Y");
    

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

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

  • -1

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

  • 11

    这个给你当地时间:

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

    这一个 UTC

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

    您可以使用简单的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以获取更多示例

  • 54

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

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

    写吧:

    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
    

    并且“回应”这个 Value ......

  • 500

    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
    
  • 2

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

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

    而格式只是Y.资本Y将是一个四位数的年份 .

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

    最高为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')
    ?>
    
  • 5

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

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

    获得全年使用:

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

    http://us2.php.net/date

    echo date('Y');
    
  • 5
    <?php echo date("Y"); ?>
    
  • 2
    <?php echo date("Y"); ?>
    

    这段代码应该这样做

  • 2

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

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

    或者在实例化时使用类成员访问的单行(php> = 5.4):

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

    对于4位数字表示:

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

    2位数字表示:

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

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

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

相关问题