<p>You can use Twig's date filter:</p>

<pre><code>{{ "now"|date('Y') }} Sitename. All rights reserved.
</code></pre>

<p><a href="http://twig.sensiolabs.org/doc/filters/date.html" rel="noreferrer">More info in the Twig docs</a></p>

CC BY-SA 3.0
2015-01-21T04:01:41.957Z
I assume i need to use it with the if else statement? where do i account for the start year?
juju
CC BY-SA 3.0
2015-01-21T04:19:09.560Z
Why do you need the if statement? Do you just want to get the current year?
Patrick Nesbitt
CC BY-SA 3.0
2015-01-21T03:54:44.700Z
2017-10-13T00:30:49.657Z
2017-10-13T00:30:49.657Z
Patrick Nesbitt
<p>The first two lines of code determine the <strong>start year</strong> and the <strong>current year</strong>.</p>

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

<p>Finally, we simply output <code>copyrightYears</code> along with the rest of the copyright text.</p>



<pre class="lang-none prettyprint-override"><code>{% set startYear = 2010 %}
{% set currentYear = now.year %}

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

&lt;p&gt;&amp;copy; {{ copyrightYears }} Site Name. All Rights Reserved&lt;/p&gt;
</code></pre>

<p>You may want to quickly read about the native <a href="http://buildwithcraft.com/docs/templating/datetime#year" rel="noreferrer"><code>now</code> variable</a>, a DateTime object which contains data about this exact moment in time (per your timezone).</p>

<p>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:</p>

<ul>
<li><a href="http://twig.sensiolabs.org/doc/templates.html#other-operators" rel="noreferrer">Ternary statements</a></li>
<li><a href="http://twig.sensiolabs.org/doc/tags/macro.html" rel="noreferrer">Macros</a></li>
</ul>

<p><strong>Example of using a ternary statement:</strong></p>

<pre class="lang-none prettyprint-override"><code>{% set copyrightYears = ((startYear &lt; currentYear) ? (startYear ~ ' - ' ~ currentYear) : startYear) %}
</code></pre>

<p><strong>Example of using a macro:</strong></p>

<pre class="lang-none prettyprint-override"><code>&lt;p&gt;&amp;copy; {{ myMacroSet.myMacro(2010) }} Site Name. All Rights Reserved&lt;/p&gt;
</code></pre>

<p>Of course, don't forget to <a href="http://twig.sensiolabs.org/doc/tags/import.html" rel="noreferrer"><code>import</code></a> "myMacroSet" before you call it!</p>

CC BY-SA 3.0
2015-01-21T13:26:02.673Z
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
CC BY-SA 3.0
2015-01-21T13:28:57.027Z
Just a thought - is it possible to set {{ copyrightYears }} as a global variable in craft? thus removing all that from the template?
juju
CC BY-SA 3.0
2015-01-21T17:00:06.803Z
@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 ;)
Douglas McDonald
CC BY-SA 3.0
2015-01-21T17:32:52.760Z
@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
CC BY-SA 3.0
2015-01-21T17:35:03.773Z
@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
CC BY-SA 3.0
2015-01-21T17:50:45.087Z
A macro is only needed if you want to set the start year as a different value on every template. I would assume with something like copyright that you would want it to be set globally for the entire site. If so, then you might want to use an `include` and/or place the start year as a `global` variable.
Douglas McDonald
CC BY-SA 3.0
2015-01-21T17:53:06.160Z
@Douglas Another solid option, great call!
Lindsey D
CC BY-SA 3.0
2015-01-21T07:14:56.283Z
2015-01-21T17:29:43.383Z
2015-01-21T17:29:43.383Z
Lindsey D
<p>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>

<pre class="lang-php prettyprint-override"><code>&lt;p&gt;
    &lt;?
      $startyear = 2010;
      $currentyear = date("Y");

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

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

    }
      ?&gt; 
&lt;/p&gt;
</code></pre>

CC BY-SA 3.0
2015-01-21T04:14:13.763Z
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
CC BY-SA 3.0
2015-01-21T06:13:42.110Z
@BradBell `$startYear - $currentYear` will be `2010 - 2015`. There's no subtraction, it's just a string.
Marion Newlevant
CC BY-SA 3.0
2015-01-21T06:17:02.103Z
Haha... sorry. Long day. :)
Brad Bell
CC BY-SA 4.0
2020-03-05T13:57:15.127Z
P.S. Don't use short php opening tag - it's depricated.
MilanG
<templating><php>
Display a copyright year (or range)
# Display a copyright year (or range)

(Asked by: username_0 on 2015-01-21)

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.

**Tags:** templating, php
> **Comments:**
> **username_1**: 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"`  
> **username_2**: @BradBell `$startYear - $currentYear` will be `2010 - 2015`. There's no subtraction, it's just a string.  
> **username_1**: Haha... sorry. Long day. :)  
> **username_3**: P.S. Don't use short php opening tag - it's depricated.  

---

## 2 answers

### Answer by username_5 on 2015-01-21. Score: 11 | Accepted answer

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 %}

© {{ copyrightYears }} Site Name. All Rights Reserved

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:

Ternary statements
Macros

Example of using a ternary statement:
{% set copyrightYears = ((startYear < currentYear) ? (startYear ~ ' - ' ~ currentYear) : startYear) %}

Example of using a macro:
© {{ myMacroSet.myMacro(2010) }} Site Name. All Rights Reserved

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

> **Comments:**
> **username_0**: 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).  
> **username_0**: Just a thought - is it possible to set {{ copyrightYears }} as a global variable in craft? thus removing all that from the template?  
> **username_6**: @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 ;)  
> **username_5**: @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. :)  
> **username_5**: @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.  
> **username_6**: A macro is only needed if you want to set the start year as a different value on every template. I would assume with something like copyright that you would want it to be set globally for the entire site. If so, then you might want to use an `include` and/or place the start year as a `global` variable.  
> **username_5**: @Douglas Another solid option, great call!  

---

### Answer by username_4 on 2015-01-21. Score: 18

You can use Twig's date filter:
{{ "now"|date('Y') }} Sitename. All rights reserved.

More info in the Twig docs

> **Comments:**
> **username_0**: I assume i need to use it with the if else statement? where do i account for the start year?  
> **username_4**: Why do you need the if statement? Do you just want to get the current year?  

---

