3

I'm using the "Thank-you letters - print or email" functionality to generate a receipt for contributions. I created a new template with all the information needed. However we have a problem to display the receipt date at the correct format. Actually, when I chose the token "Receipt date" in the combobox, Civi inserts {contribution.receipt_date} in my template, and when I generate a PDF, it displays the date with the hours : for example, "2019-02-25 16:01:00". We would just like to display the date (without the hours) at this format : dd/mm/YYYY (for example : "25/02/2019"). I tried to add code like "date_format" in the source of the template to change the date format, but it didn't work. Is there an easy way to change it ?

Thanks !

1
  • I follow your guide but the date it gives me is today date and not the receive_date... Any suggest? Thank you
    – Angela
    Commented May 26, 2021 at 14:52

4 Answers 4

6

If you want 28 February 2019 {foreach from=$contributions item=contribution} {assign var="date" value=$contribution.receive_date|date_format:"%d %B %Y"}

If you want 28 02 2019 {foreach from=$contributions item=contribution} {assign var="date" value=$contribution.receive_date|date_format:"%d %m %Y"}

If you want it to look better (28/02/2019) you can add slashes value=$contribution.receive_date|date_format:"%d/%m/%Y"}

5
  • Thank you for your quick answer ! Am I supposed to put this code directly in the template (so in the civi interface) or in a file ?
    – Davum
    Commented Mar 1, 2019 at 9:07
  • 1
    Correct, put it in the template. Admin > Communications > Message Templates. I assume there is already some Smarty code there. You will likely just need to edit that code to include the last part |date_format...
    – Iowa Boy
    Commented Mar 1, 2019 at 16:23
  • 1
    Thank you for your answer again. I put the code ("|date_format...") in the message template, but it displays "{contribution.receipt_date|date_format:"%d %m %Y"}" when I generate the PDF instead of the value of the date. It's like it wasn't able to understand the smarty code... even though we're using smarty in other templates and it works properly. Have you any ideas of what I could have probably forgotten ?
    – Davum
    Commented Mar 4, 2019 at 10:10
  • Where you have {contribution.receipt_date|date_format:"%d %m %Y"} should just have {date} the smarty is further up on your template. Look for something like {foreach from=$contributions item=contribution} {assign var="date" value=$contribution.receive_date} and add/edit the |date_format:"%d %m %Y"
    – Iowa Boy
    Commented Mar 4, 2019 at 18:15
  • 1
    I think part of the confusion here is that {contribution.receipt_date}, i.e. "recei*pt*" and without the $ sign, is a token and smarty modifiers won't work on it, whereas {$contribution.receive_date}, i.e. "recei*v*e" and with the $ sign, is a variable available for use in the template, which you can use smarty modifiers on.
    – Demerit
    Commented Mar 5, 2019 at 1:51
3

As of CiviCRM 5.43, there is a much simpler answer:

{contribution.receipt_date|crmDate:"shortdate"}

There is full documentation for the crmDate filter.

2

You will need to have Smarty template processing turned on for mailings: make sure CIVICRM_MAIL_SMARTY is defined in your civicrm.settings.php file, as described in the CiviCRM User Guide.

Then, replace the {contribution.receive_date} token in your message with Smarty code such as this:

  • For a date like "26/01/2020" (dd/mm/yyyy):

    {capture assign=formattableDateReceived}{contribution.receive_date}{/capture}{capture assign=formatString}%d/%m/%Y{/capture}{$formattableDateReceived|date_format:$formatString}

  • For a date like "December 7, 2020":

    {capture assign=formattableDateReceived}{contribution.receive_date}{/capture}{capture assign=formatString}%B %e, %Y{/capture}{$formattableDateReceived|date_format:$formatString|strip}

  • For a date like "01/26/2020" (mm/dd/yyyy):

    {capture assign=formattableDateReceived}{contribution.receive_date}{/capture}{capture assign=formatString}%m/%d/%Y{/capture}{$formattableDateReceived|date_format:$formatString}

  • For a date like "1/26/2020" (m/d/yyyy):

    {capture assign=formattableDateReceived}{contribution.receive_date}{/capture}{capture assign=formatString}%m/%d/%Y{/capture}{capture assign=mdyCleanupFind}#(^|/)0#{/capture}{capture assign=mdyCleanupReplace}$1{/capture}{$formattableDateReceived|date_format:$formatString|regex_replace:$mdyCleanupFind:$mdyCleanupReplace}

These four examples use Smarty's date_format, strip and regex_replace modifiers. Due to the way that Civi applies Smarty to thank-you letters (as of Civi 5.32), we have to capture the parameters for each of these modifiers in a variable before using them.

The patterns above should work for other date fields as well (receipt_date, cancel_date etc.)

2
  • 1
    To further explain why we can't give arguments to the Smarty modifiers directly as quoted strings: as of Civi 5.32, in CRM_Contribute_Form_Task_PDFLetterCommon::resolveTokens, the Smarty compiler is only called after Civi has already HTML-ified all the quotes within our Smarty tags, breaking them completely. Commented Jan 26, 2021 at 7:33
  • this worked for me! thanks!
    – PTP Mark
    Commented May 20, 2021 at 17:40
0

This code helped me to include an European date format into PDF-invoices:

{$invoice_date|date_format:"%d.%m.%Y"}

in your case you have to change invoice_date with the desired contribution.receipt_date

1
  • Note that in php 8.1 this is deprecated because date_format uses strftime(). At some point you'll need to switch to crmDate as other answers indicate.
    – Demerit
    Commented Nov 3, 2022 at 18:30

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.