How do I use PHP to get the current year?

后端 未结 26 2229
萌比男神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 05:01

    Get full Year used:

     <?php 
        echo $curr_year = date('Y'); // it will display full year ex. 2017
    ?>
    

    Or get only two digit of year used like this:

     <?php 
        echo $curr_year = date('y'); // it will display short 2 digit year ex. 17
    ?>
    
    0 讨论(0)
  • 2020-12-04 05:02

    This one gives you the local time:

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

    And this one UTC:

    $year = gmdate('Y'); // 2008
    
    0 讨论(0)
  • 2020-12-04 05:04

    You can use either date or strftime. In this case I'd say it doesn't matter as a year is a year, no matter what (unless there's a locale that formats the year differently?)

    For example:

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

    On a side note, when formatting dates in PHP it matters when you want to format your date in a different locale than your default. If so, you have to use setlocale and strftime. According to the php manual on date:

    To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().

    From this point of view, I think it would be best to use strftime as much as possible, if you even have a remote possibility of having to localize your application. If that's not an issue, pick the one you like best.

    0 讨论(0)
  • 2020-12-04 05:04
    $year = date("Y", strtotime($yourDateVar));
    
    0 讨论(0)
  • 2020-12-04 05:05
    print date('Y');
    

    For more information, check date() function documentation: https://secure.php.net/manual/en/function.date.php

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

    best shortcode for this section:

    <?= date("Y"); ?>
    
    0 讨论(0)
提交回复
热议问题