Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-21T04:01:41.957Z
Answers.Text: I assume i need to use it with the if else statement? where do i account for the start year?
Answers.UserDisplayName: juju
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-21T04:19:09.560Z
Answers.Text: Why do you need the if statement? Do you just want to get the current year?
Answers.UserDisplayName: Patrick Nesbitt
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-21T03:54:44.700Z
Answers.LastActivityDate: 2017-10-13T00:30:49.657Z
Answers.LastEditDate: 2017-10-13T00:30:49.657Z
Answers.UserDisplayName: Patrick Nesbitt
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-21T13:26:02.673Z
Answers.Text: 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).
Answers.UserDisplayName: juju
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-21T13:28:57.027Z
Answers.Text: Just a thought - is it possible to set {{ copyrightYears }} as a global variable in craft? thus removing all that from the template?
Answers.UserDisplayName: juju
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-21T17:00:06.803Z
Answers.Text: @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 ;)
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-21T17:32:52.760Z
Answers.Text: @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. :)
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-21T17:35:03.773Z
Answers.Text: @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.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-21T17:50:45.087Z
Answers.Text: 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.
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-21T17:53:06.160Z
Answers.Text: @Douglas Another solid option, great call!
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-21T07:14:56.283Z
Answers.LastActivityDate: 2015-01-21T17:29:43.383Z
Answers.LastEditDate: 2015-01-21T17:29:43.383Z
Answers.UserDisplayName: Lindsey D
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-01-21T04:14:13.763Z
Comments.Text: 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"`
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-01-21T06:13:42.110Z
Comments.Text: @BradBell `$startYear - $currentYear` will be `2010 - 2015`. There's no subtraction, it's just a string.
Comments.UserDisplayName: Marion Newlevant
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-01-21T06:17:02.103Z
Comments.Text: Haha... sorry. Long day. :)
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-03-05T13:57:15.127Z
Comments.Text: P.S. Don't use short php opening tag - it's depricated.
Comments.UserDisplayName: MilanG
Tags: <templating><php>
Title: Display a copyright year (or range)
ThreadText: # 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.
  
  ```php
  <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>
  
  ```
  
  **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**: @username_1 `$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.
  
  ```none
  {% 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](http://buildwithcraft.com/docs/templating/datetime#year), 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](http://twig.sensiolabs.org/doc/templates.html#other-operators)
  * [Macros](http://twig.sensiolabs.org/doc/tags/macro.html)
  
  **Example of using a ternary statement:**
  
  ```none
  {% set copyrightYears = ((startYear < currentYear) ? (startYear ~ ' - ' ~ currentYear) : startYear) %}
  
  ```
  
  **Example of using a macro:**
  
  ```none
  <p>&copy; {{ myMacroSet.myMacro(2010) }} Site Name. All Rights Reserved</p>
  
  ```
  
  Of course, don't forget to [`import`](http://twig.sensiolabs.org/doc/tags/import.html) "myMacroSet" before you call it!
  
  > **Comments:**
  > **username_0**: username_5 - 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**: @username_0 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**: @username_6 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**: @username_0 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**: @username_6 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](http://twig.sensiolabs.org/doc/filters/date.html)
  
  > **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?  
  
  ---
  
  