How do I use PHP to get the current year?

后端 未结 26 2227
萌比男神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:06

    Just write:

    date("Y") // A full numeric representation of a year, 4 digits
              // Examples: 1999 or 2003
    

    Or:

    date("y"); // A two digit representation of a year     Examples: 99 or 03
    

    And 'echo' this value...

    0 讨论(0)
  • For up to php 5.4+

    <?php
        $current= new \DateTime();
        $future = new \DateTime('+ 1 years');
    
        echo $current->format('Y'); 
        //For 4 digit ('Y') for 2 digit ('y')
    ?>
    

    Or you can use it with one line

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

    If you wanna increase or decrease the year another method; add modify line like below.

    <?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')
    ?>
    
    0 讨论(0)
  • 2020-12-04 05:12
    <?php echo date("Y"); ?>
    

    This code should do

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

    With PHP heading in a more object-oriented direction, I'm surprised nobody here has referenced the built-in DateTime class:

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

    or one-liner with class member access on instantiation (php>=5.4):

    $year = (new DateTime)->format("Y");
    
    0 讨论(0)
  • 2020-12-04 05:14

    in my case the copyright notice in the footer of a wordpress web site needed updating.

    thought simple, but involved a step or more thann anticipated.

    1. Open footer.php in your theme's folder.

    2. Locate copyright text, expected this to be all hard coded but found:

      <div id="copyright">
          <?php the_field('copyright_disclaimer', 'options'); ?>
      </div>
      
    3. Now we know the year is written somewhere in WordPress admin so locate that to delete the year written text. In WP-Admin, go to Options on the left main admin menu:

      Then on next page go to the tab Disclaimers:

      and near the top you will find Copyright year:

      DELETE the © symbol + year + the empty space following the year, then save your page with Update button at top-right of page.

    4. With text version of year now delete, we can go and add our year that updates automatically with PHP. Go back to chunk of code in STEP 2 found in footer.php and update that to this:

      <div id="copyright">
          &copy;<?php echo date("Y"); ?> <?php the_field('copyright_disclaimer', 'options'); ?>
      </div>
      
    5. Done! Just need to test to ensure changes have taken effect as expected.

    this might not be the same case for many, however we've come across this pattern among quite a number of our client sites and thought it would be best to document here.

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

    If you are using the Carbon PHP API extension for DateTime, you can achieve it easy:

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

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