7

I want to calculate the remaining days between the current day (now) and a future date set as a date field in {{ inschrijven.datumEvenement }} globals

{{ inschrijven.datumEvenement.diff(now).format('%d') }}

This is what I came up with but it doesn't generate the right number of days left.

Using now = 05-15-2015, inschrijven.datumEvenement = 08-30-2015 the above tag generates "14".

7

You want to use "a" as your DateInterval format parameter instead of "d".

Also, you may need to use Twig's date function on the end date, which "converts an argument to a date to allow date comparison".

This should give you the correct number of days between now and the future date:

{{ date( inschrijven.datumEvenement|date ).diff( now ).format('%a') }}

or

{{ date( inschrijven.datumEvenement|date ).diff( now ).days }}
| improve this answer | |
  • Dylan thanks, your suggestion did the job! 106 days to go.. – Robbie Post May 15 '15 at 19:12
  • @Robbie if inschrijven.datumEvenement is from a Date/Time field than it is already a DateTime object and you don't need the date filter or date function. But even if it is not, you never need both! :D – carlcs May 15 '15 at 21:32
  • @carlcs I thought so too but I was seeing incorrect results without the date function. For example {{ globalSet.dateField.diff( now ).format('%a') }} resulted in (unknown) and {{ globalSet.dateField.diff( now ).days }} outputs -99999. And using the date function without the date filter also produced the same results. Do you know why that would be? – Dylan T May 15 '15 at 21:56
  • Hmm.. it could be possible that Craft DateTime objects don't do the diff method or do something wrong, and the date filter converts them to normal Twig DateTime objects? – carlcs May 15 '15 at 22:11
  • Ok, it is, as I guessed, a bug with the Craft DateTime object. Normal DateTime object you create using Twig's date function work just fine: date('2015-05-01').diff(date('2015-05-04')).days. What you actually do with combining date filter and function is to return the Craft DateTime as a string and then make that a normal Twig DateTime using the function. One thing to note to that "workaround" is that you should better return the DateTime including the time and the timezone with the filter, e.g. date(myDate|date('c')) or date(myDate|date('U')). – carlcs May 15 '15 at 23:18
2

This makes sense. The output of "diff" is a date object and you are requesting the days of the formatted date (03-15-0000), in this case 15 days.

Your best bet is to extract the days from the object rather than from the formatted date. Try...

{{ inschrijven.datumEvenement.diff(now).days }}

| improve this answer | |
2

This will work if you want to diff from now.

{% set leftDays = meet.dateStart.diff( date("now") ).format('%a') %}
| improve this answer | |

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.