Can I do the 'th' Date Formatting in a PHP Server Action?

I’d like to be able to format a date to have the ‘th’ component in a PHP based server action… (so ‘4th August’ rather than ‘4 August’)… is that possible to do?

Good shout. That’s something that’s always been lacking and I’ve just lived without but would be great to have it added to the date formatter.

1 Like

No build in formatter for that, but you can add this using conditions in the server action.
Here are the steps required:

Explanation:

  1. Split the formatted (d MMMM) date to day and month vars
  2. Check if the date is 11,12 or 13 -> then add th
  3. If the number ends in 1, add “st” (e.g., 1st, 21st, 31st).
    If the number ends in 2, add “nd” (e.g., 2nd, 22nd).
    If the number ends in 3, add “rd” (e.g., 3rd, 23rd).
    For all other cases, add “th” (e.g., 4th, 11th, 12th, 13th, 24th).
    And here’s the result:

Screenshot 2023-08-02 at 11.48.09

4 Likes

Should be an easy custom formatter for anyone who thinks it is important

4 Likes

try {{NOW.formatDate('dS MMMM', false)}}

1 Like

WORKS! :tada:

Love you Patrick…

PS - Be amazing if that was added here:

(Here you go @sitestreet and @teodor…)

@patrick, is that PHP specific or does it work with Node too? :thinking:

It is PHP specific, does not work in Node.

Brilliant. Thanks again @patrick.

Could this be added to the App Connect formatter? I usually do my date formatting client-side.

Here you go @sitestreet:tada:

Add this to your code…

  <!-- https://community.wappler.io/t/how-to-get-date-with-st-nd-or-th-using-formatdate/14686/6 -->
  <script>
    dmx.Formatter('string', 'formatDate2', function(val, format) {
  return moment(val).format(format);
});
  </script>

Then use:

.formatDate2('Do')

How to get date with st nd or th using formatdate

1 Like

@teodor, I changed it out of the extensions category as it is clearly a part of the core product!