4

I have an old php code in the footer of my site like below. Will like to convert that to twig (which I suspect will be cleaner). I don't know a lick of php hence my question here.

<p>
    <?
      $startyear = 2010;
      $currentyear = date("Y");

      if ($startyear == $currentyear)
   {
      echo "&copy; $startyear Site Name. All Rights Reserved\n";

      }
  else
   {
     echo "&copy; $startyear - $currentyear Sitename. All Rights Reserved\n";

    }
      ?> 
</p>
4
  • I'm confused by the PHP code. If $startYear is 2010 and $currentYear is 2015, then $startYear - $currentYear is going to be -5. So you'd get &copy; -4 Sitename. All Rights Reserved"
    – Brad Bell
    Commented Jan 21, 2015 at 4:14
  • @BradBell $startYear - $currentYear will be 2010 - 2015. There's no subtraction, it's just a string. Commented Jan 21, 2015 at 6:13
  • 1
    Haha... sorry. Long day. :)
    – Brad Bell
    Commented Jan 21, 2015 at 6:17
  • P.S. Don't use short php opening tag - it's depricated.
    – MilanG
    Commented Mar 5, 2020 at 13:57

2 Answers 2

11

The first two lines of code determine the start year and the current year.

The next block of code compares the two years, and sets the copyrightYears variable accordingly.

Finally, we simply output copyrightYears along with the rest of the copyright text.

{% set startYear = 2010 %}
{% set currentYear = now.year %}

{% if startYear == currentYear %}
    {% set copyrightYears = startYear %}
{% else %}
    {% set copyrightYears = startYear ~ ' - ' ~ currentYear %}
{% endif %}

<p>&copy; {{ copyrightYears }} Site Name. All Rights Reserved</p>

You may want to quickly read about the native now variable, a DateTime object which contains data about this exact moment in time (per your timezone).

It's also worth mentioning... There are a few shorter ways to do this, this example was written with the purpose of breaking down each step individually. If you'd like to trim this code down further, I'd suggest looking into one or both of these Twig capabilities:

Example of using a ternary statement:

{% set copyrightYears = ((startYear < currentYear) ? (startYear ~ ' - ' ~ currentYear) : startYear) %}

Example of using a macro:

<p>&copy; {{ myMacroSet.myMacro(2010) }} Site Name. All Rights Reserved</p>

Of course, don't forget to import "myMacroSet" before you call it!

7
  • Lindsey - this is great. thx. I will take it as a small challenge to see if i can trim it further like you suggest. I wonder how much trimmer this can get (perhaps something to eliminate the conditional statements).
    – juju
    Commented Jan 21, 2015 at 13:26
  • Just a thought - is it possible to set {{ copyrightYears }} as a global variable in craft? thus removing all that from the template?
    – juju
    Commented Jan 21, 2015 at 13:28
  • @juju if the start year is always 2010 then you can simply write <p>&copy; 2010 - {{ now|date('Y') }} Site Name. All Rights Reserved</p>. One of the few things I know for sure is that it will never be 2010 again ;) Commented Jan 21, 2015 at 17:00
  • @Douglas I'd imagine that the purpose of this code is for future re-use. Though you do make a good point... it'll never be 2010 again. :)
    – Lindsey D
    Commented Jan 21, 2015 at 17:32
  • @juju It sounds like what you're after is a macro (good choice, I'd probably do the same). That's the best way for you to move that code to a more recyclable location.
    – Lindsey D
    Commented Jan 21, 2015 at 17:35
18

You can use Twig's date filter:

{{ "now"|date('Y') }} Sitename. All rights reserved.

More info in the Twig docs

2
  • I assume i need to use it with the if else statement? where do i account for the start year?
    – juju
    Commented Jan 21, 2015 at 4:01
  • Why do you need the if statement? Do you just want to get the current year? Commented Jan 21, 2015 at 4:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.