[PHP/JS] コピーライト表記の西暦を自動更新する

記事内にプロモーションを含む場合があります。

Dynamic timestamp

ページのフッターなどに表示する「コピーライト©表記」の西暦を自動更新させる方法。

JapaScriptとPHPの2通りの方法があり、WordPressやMovableTypeで拡張子をphpにしている場合は、PHPが使用できます。

PHPを使う方法

© 2015

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

© 2008-2015 Company.

&copy; <?php 
  $fromYear = 2008; 
  $thisYear = (int)date('Y'); 
  echo $fromYear . (($fromYear != $thisYear) ? '-' . $thisYear : '');?> Company.

JavaScriptを使う方法

©

&copy; <script type="text/javascript">
  document.write(new Date().getFullYear());
</script>

© 2010-2015, Company.

&copy; 2010<script>new Date().getFullYear()>2010&&document.write("-"+new Date().getFullYear());</script>, Company.

Source: Update Your Footer.