Code Example - Drupal

This is an example of using PHP within an HTML page such that Drupal will execute the PHP and return the resulting HTML to the browser. Drupal 7 allows you to have "PHP code" as an option for a Text Format, this was removed in Drupal 8 for security reasons, but you can add it back via a module.

The intention of this code is that you can state the starting year and then it works out a suitable copyright notice. For example, in 2012 the output was "Copyright © Geoff Lawrence 2012" but in 2020 the output is "Copyright © Geoff Lawrence 2012-20". It is nothing especially clever, just convenient as it updates the year for you.

<div>Copyright &copy; Geoff Lawrence 
<?php
function getCopyrightYears($baseyear)
//returns either "$baseyear" or "$baseyear-$thisyear" depending on current year
{
  $today = getdate();
  $thisyear = $today['year'];

  if ($baseyear > $thisyear) {
    $baseyear = $thisyear;
  }
  $result = (string)$baseyear;
  if ($baseyear != $thisyear) {
    $result .= sprintf('-%s', date('y'));
  }
  return $result;
}

echo getCopyRightYears(2012);
?>
</div>

The actual HTML return from this PHP is as follows:

<div>Copyright © Geoff Lawrence 
2012-20</div>

Similar results can be achieved with JavaScript, however they run client-side and hence have a different performance impact.