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
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.
-
Just adding that even if date_format worked, it's deprecated since it uses strftime() which is deprecated in php.– DemeritMar 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}
– DemeritMar 29 at 21:38 -
1Demerit, as always, you're a magician. I got the capture version to work!!! SUPER helpful! I will edit my answer to reflect your additions. Mar 31 at 22:19
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!