1

I was going through my code for testing purposes and found out, that When entry's first time created, it also outputs data from the field "dateUpdated". I'm assuming, when an entry is first time created it is also first time updated.

So I'm trying to write a conditional statement to not output "dateUpdated" for entries, that were first time created.

So far I didn't come up with anything, because I'm not sure If what I'm assuming is true.

For the start.

{# Date #}
<span>
     {{ "published"|t|ucwords }}: {{ entry.dateCreated is empty ? "" : entry.dateCreated|date("d.m.Y") }},
</span>
{% if entry.dateUpdated %}
      <span class="uk-text-success-alert">
            {{ "updated"|t|ucwords }}: {{ entry.dateUpdated is empty ? "" : entry.dateUpdated|date("d.m.Y") }},
      </span>
{% endif %}
{% if entry.expiryDate %}
      <span class="uk-text-danger">
            {{ "expire"|t|ucwords }}: {{ entry.expiryDate is empty ? "" : entry.expiryDate|date("d.m.Y") }},
      </span>  
{% endif %} 
5
  • I'm relatively new to the Craft scene, so forgive my naivety. However, just poking around the DB after an install and after creating entries etc I noticed that dateCreated and dateUpdated are entered at the same time. So, instead of checking for a value, could you check if dateUpdated is after dateCreated?
    – Damon
    Apr 20 '15 at 14:24
  • The values must to be the same as they are populated at the same time. Apr 20 '15 at 14:40
  • Or you mean, the position in the row of the entry field? If so, Yes it is. Apr 20 '15 at 14:42
  • It's entirely possible I'm reading your question wrong, but if you only want to show the updated if in fact the record was updated and not initially created, then you would just check the two dates - and if the datedUpdated date is after the dateCreated then that should work I think...
    – Damon
    Apr 20 '15 at 14:44
  • Well, I only need to output the dateCreated and in condition dateUpdated and expiryDate for entry. So I have to write some conditional statement, where I compare times. It must to be easy, just hard to start. Apr 20 '15 at 14:51
2

Here's where I'm going to be more help in theory, rather than code. But maybe something like this? I'm happy to revise if someone sees a typo.

{# Date #}
<span>
     {{ "published"|t|ucwords }}: {{ entry.dateCreated is empty ? "" : entry.dateCreated|date("d.m.Y") }},
</span>
{% if entry.dateCreated != entry.dateUpdated %}
      <span class="uk-text-success-alert">
            {{ "updated"|t|ucwords }}: {{ entry.dateUpdated is empty ? "" : entry.dateUpdated|date("d.m.Y") }},
      </span>
{% endif %}
{% if entry.expiryDate|length %}
      <span class="uk-text-danger">
            {{ "expire"|t|ucwords }}: {{ entry.expiryDate is empty ? "" : entry.expiryDate|date("d.m.Y") }},
      </span>  
{% endif %}
3
  • Haven't encountered on any problem yet. If I don't, I tick this answer for the correct one. Nice one. Thanks @dragonslovetacos Apr 20 '15 at 15:21
  • Awesome! Let me know if you do - my twig/craft skills are still at level "rookie" :)
    – Damon
    Apr 20 '15 at 15:35
  • I'm somewhere at "half-rookie" level :) A long way ahead! Anyway, comparison understood, it gives the logic and most important IT WORKS, because human mind has built it :) Thank you again @dragonslovetacos Apr 20 '15 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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