How do I use PHP to get the current year?

后端 未结 26 2225
萌比男神i
萌比男神i 2020-12-04 04:31

I want to put a copyright notice in the footer of a web site, but I think it\'s incredibly tacky for the year to be outdated.

How would I make the year update automat

相关标签:
26条回答
  • 2020-12-04 04:57
    <?php echo date("Y"); ?>
    
    0 讨论(0)
  • 2020-12-04 04:58

    My super lazy version of showing a copyright line, that automatically stays updated:

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

    This year (2008), it will say:

    © 2008 Me, Inc.

    Next year, it will say:

    © 2008-2009 Me, Inc.

    and forever stay updated with the current year.


    Or (PHP 5.3.0+) a compact way to do it using an anonymous function so you don't have variables leaking out and don't repeat code/constants:

    &copy; 
    <?php call_user_func(function($y){$c=date('Y');echo $y.(($y!=$c)?'-'.$c:'');}, 2008); ?> 
    Me, Inc.
    
    0 讨论(0)
  • 2020-12-04 04:58
    strftime("%Y");
    

    I love strftime. It's a great function for grabbing/recombining chunks of dates/times.

    Plus it respects locale settings which the date function doesn't do.

    0 讨论(0)
  • 2020-12-04 04:58

    For 4 digit representation:

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

    2 digit representation:

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

    Check the php documentation for more info: https://secure.php.net/manual/en/function.date.php

    0 讨论(0)
  • 2020-12-04 04:58

    use a PHP function which is just called date().

    It takes the current date and then you provide a format to it

    and the format is just going to be Y. Capital Y is going to be a four digit year.

    <?php echo date("Y"); ?>
    
    0 讨论(0)
  • 2020-12-04 05:01

    echo date('Y') gives you current year, and this will update automatically since date() give us the current date.

    0 讨论(0)
提交回复
热议问题