XF 2.1 Date calculation

makaiguy

Member
I have a need to show as a string a date XX days in the future, formatted as MONTHNAME date, year.

I'm currently doing so via:
  1. Placing an HTML id'ed div which contains a default string into the template .
  2. Calling a short javascript to calculate the future date, format it as desired and replace the default string in the template via
    JavaScript:
    document.getElementById("div_id").innerHTML = new_date_string

Is there any way to do this more easily using XF's own internal date/time capabilities?
 
Last edited:
I don't think so, as you'd need to pass a Unix timestamp or a \DateTime object to the templaters date functions, and the only facility for creating a \DateTime object from the templater is date_from_format(), which doesn't support relative time.
 
Thanks, Jeremy. I kinda figured that.

The reason I'm trying to find an alternate method is that I need to display this same thing in more than one template, so I'm doing it by having the code in a separate entity, then calling it as needed in several templates. This way I only have to maintain the code in one place.

I set this up via a custom phrase, and it works as desired when only used once in a give template. I have one template, though, where I want the date string to be displayed in two different locations, but the second instance displays the default phrase rather than the desired date-calculated string that is supposed to replace it. It's as though the js isn't running at all the second time through. I've tried both including the script directly within the phrase and loading it from a separate js file. Both give the same result.

I've just changed over to having the XF code located in a new custom template, and inserting that into the appropriate places, but with the same result. Works great the first time in a given template, but not the second.

I'm thinking there may be a way to define the date string one time in the template and storing it in some sort of string variable, then displaying it one or more times as needed, but I haven't a clue how to go about doing either part of it: creating the string variable (presumably using js to then set its value), or then displaying the value of the stored string .

Any suggestions?
 
I have one template, though, where I want the date string to be displayed in two different locations, but the second instance displays the default phrase rather than the desired date-calculated string that is supposed to replace it. It's as though the js isn't running at all the second time through. I've tried both including the script directly within the phrase and loading it from a separate js file. Both give the same result.
Judging from the snippet above, it's likely because you're selecting a single element by its ID. IDs must be unique (one per page), so maybe use a class instead and be sure to select all matching elements.

In jQuery:
Code:
$('.className').text('Your replacement text');

Or if you're avoiding jQuery for whatever reason:
Code:
document.querySelectorAll('.className').forEach(element => element.textContent = 'Your replacement text');

A better approach might be using a template callback so that it isn't dependent on JavaScript.
 
Actually, I suppose you could create a relative timestamp by just adding to $xf.time:

HTML:
{{ date($xf.time + 86400 * 3) }}

You can pass an optional format string as a second parameter if the default isn't what you want.
 
Actually, I suppose you could create a relative timestamp by just adding to $xf.time:

HTML:
{{ date($xf.time + 86400 * 3) }}

You can pass an optional format string as a second parameter if the default isn't what you want.
That's great, works a treat and eliminates some code to boot. We're almost there.

I've searched but could not find a listing of the date format parameters. M gives me a 3-letter month abbreviation. Trying MMM to get the full spelled out month just gave me the abbreviation 3 times. Is there a parameter code for the month name? I hate to do too much trial and error experimenting for something fairly trivial like this because each one freezes the board for all my users while things recompile.
 
Last edited:
That's great, works a treat and eliminates some code to boot. We're almost there.

I've searched but could not find a listing of the date format parameters. M gives me a 3-letter month abbreviation. Trying MMM to get the full spelled out month just gave me the abbreviation 3 times. Is there a parameter code for the month name? I hate to do too much trial and error experimenting for something fairly trivial like this because each one freezes the board for all my users while things recompile.

It's the PHP date function format, the formatting characters are listed on its documentation page: https://www.php.net/manual/en/function.date.php

For a month, the character is F.
 
Last edited:
PHP! Of COURSE that's what I should have been looking for! doh.gif
Boy, that shows how long it is since I've played with any of this stuff!

I've got it doing everything I wanted now. Thanks a gazillion guys.
 
Top Bottom