3

I'm trying to set up membership renewal reminder letters, and instead of including the entire date that the member joined, we'd like it to just be the year. I have a token for "latestcurrentmembership.join_date" that adds that date as "January 1, 2022." Is there a way to have the token only add "2022" to the letter?

2 Answers 2

1

I had been looking for something similar and wanted to follow up for anyone reading this post. (I would have left a comment on Demerit's answer but wanted to include a tad more info.)

I could not get the token modifiers listed to work in Civi 5.57.4 on Drupal 7, PHP 8.025 (even though it says they work in 5.43 and up, perhaps I have some customization causing a conflict).

I was able to get them to work on the Dmaster today though - it is running 5.61.alpha1 on Drupal 7 and PHP 7.4.33. I found that you can use those for core date but not custom date fields, here's a printout:

Date of birth  
{contact.birth_date} : December 1st, 1980
{contact.birth_date|crmDate:"Year"} : 1980

Custom Date token
{contact.custom_3} : 03/01/2023
{contact.custom_3|crmDate:"Year"} : 03/01/2023

I'm guessing that the fact that the custom date token formats differently out of the gate is part of the reason it doesn't work.

For fun, I also tried the smarty modifier |date_format:"%Y" but that produces an error: Invalid token filter: ["date_format","%Y"] so they've shored up that part of the code to help show errors.


Additional information based on Demerit's comments. While the crmDate token modifiers didn't work for a custom field Demerit was spot on.

For the custom one, if you know the date format is always mm/dd/yyyy, you could do {capture assign=foo}{contact.custom_3}{/capture}{$foo|substring:6:4}, and actually I don't think you need the capture, just some quotes: {"{contact.custom_3}"|substring:6:4}

The capture method worked perfectly for me.

3
  • Just adding that even if date_format worked, it's deprecated since it uses strftime() which is deprecated in php.
    – Demerit
    Mar 29 at 21:31
  • For the custom one, if you know the date format is always mm/dd/yyyy, you could do {capture assign=foo}{contact.custom_3}{/capture}{$foo|substring:6:4}, and actually I don't think you need the capture, just some quotes: {"{contact.custom_3}"|substring:6:4}
    – Demerit
    Mar 29 at 21:38
  • 1
    Demerit, as always, you're a magician. I got the capture version to work!!! SUPER helpful! I will edit my answer to reflect your additions.
    – RayWright
    Mar 31 at 22:19
0

It might work to use crmDate as described at https://docs.civicrm.org/user/en/latest/common-workflows/tokens-and-mail-merge/#date, i.e.

{latestcurrentmembership.join_date|crmDate:"Year"}

However not everything listed there works everywhere in all versions so I don't know offhand if that works in your version of civi in scheduled reminders. Only one way to find out!

2
  • Thanks for the suggestion, but no luck when I tried it. I'll keep at!
    – JamesA
    Sep 22, 2022 at 18:00
  • Maybe {$latestcurrentmembership.join_date|date_format:"%Y"} ? That would however be using smarty - not sure if that works in scheduled reminders...
    – Fabian
    Oct 24, 2022 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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