XF 1.5 Custom Sidebar Block with Current Date and Time

Amaury

Well-known member
I've created a custom sidebar block to be used to display the current date and time with respect to the user's selected time zone or the time zone set for guests. I may end up changing it to two separate blocks for aesthetics reasons, but all the same, really.

Anyway, I'm not able to get it to work, so I am obviously doing something wrong. This is my current setup:

Custom Sidebar 1.webp

Custom Sidebar 2.webp

As you can see below, it doesn't work:

Custom Sidebar 3.webp

What am I doing wrong? Thanks.

Edit: A mock-up of what I'm trying to achieve.

Custom Sidebar 4.webp
 
If you want to use variables in a phrase, there must be supporting back end PHP code for them.

The template code would also look something like:
Code:
{xen:phrase phrase_name, 'date={xen:datetime $foo}', 'time={$xen:datetime $bar}'}

Otherwise you can just add the relevant code directly to the template, assuming the variables are available in that template.
 
Yes, :) However, without the "at" and a dot separator instead as seen my code, though I may end up having two separate blocks for the date and time if that's possible. Although for the "at" I realize that comes from the same the phrase that displays the time on posts, conversations, etc., and that I went left as is, so I'm not sure if that's possible.
 
Yup! Although wouldn't that just follow the format I have set in the options? M/D/YYYY. No leading zeroes and full year. I guess not, though, eh?

Also, for two separate blocks, then, would this be correct?

Code:
{xen:date $serverTime, 'm/d/yyyy'}

{xen:date $serverTime, 'g:i A'}
 
You don't want four y's ;)

If you want the full four digit year, a capital Y will do it

Code:
{xen:date $serverTime, 'm/d/Y'}

{xen:date $serverTime, 'g:i A'}

Do you mean on two lines in the same sidebar widget? Or two completely separate sidebar widgets?
 
Double post - sorry.

If you want it in the same widget, then just wrap it in some divs and give them a class so you can make it pretty :D

Rough concept:-
amaurycss.webp
Code:
<h3>Current Time and Date</h3>
<div class="amaury_date">{xen:date $serverTime, 'm/d/Y'}
</div>
<div class="amaury_time">{xen:date $serverTime, 'g:i A'}        </div>

CSS:-

Code:
.amaury_time {
    color: red;
}


.amaury_date {
    color: green;
}
Play around to your hearts content ;)
 
Ah, right. Now I remember. My brain was wrapped around OpenOffice, where M/D/YYYY is what I would want. For PHP, if I don't want leading zeroes and the full year, I want n/j/Y like what I have set in the language.

Now, one last question: I'd want to keep the phrase for the display in the template rather than have it hard-coded (unless it's not necessary in this case?), so would this be correct?

Code:
{xen:phrase phrase_name {xen:date $serverTime, 'g:i A'}}

In any case, I'll work on this later after I'm done with classes.

Thanks, Lisa. You're awesome! :)
 
All right.

In reality, with something like this, I guess it doesn't really matter. If you had just plain text like, "This is my site!" then yeah, use a phrase since you may want to change it later to forum or something and having it as a phrase will make it a whole lot easier, especially if used in multiple locations, but the current date and time is something that's essentially permanent and won't change.
 
The Display Format for dates may differ depending on your country/language, so you could store the format only into a phrase - haven't tried doing so yet though, so can't say for sure that it works, depends on what is execute first in XenForo.
 
It's not something I've ever done, but I would assume using T P in the $serverTime line somewhere should do it. I'll check when I wake up properly :)
 
@Lisa, have enough coffee yet? ;)

So I got it styled:

Code:
<div class="section">
    <div class="secondaryContent">
        <h3>{xen:phrase khflare_current_date}</h3>
        <center><font size="3">{xen:date $serverTime, 'n/j/Y'}</font></center>
    </div>
</div>

<div class="section">
    <div class="secondaryContent">
        <h3>{xen:phrase khflare_current_time}</h3>
        <center><font size="3">{xen:date $serverTime, 'g:i A'}</font></center>
    </div>
</div>

Sidebar 1.webp

However, I then got to thinking that that clutters things up a bit (even if it had been the original idea of the date and time in the same block) and makes the sidebar unbalanced.--longer than the categories and forums. So I thought of adding the current date and time to the Forum Statistics block. (They technically count as forum statistics, yes?) What do you think? I feel that looks a little better myself.

Sidebar 2.webp

Under the time would be the time zone (GMT -8 or -7 in my case).
 
@Lisa, have enough coffee yet? ;)
Is that even possible. Sorry, one of the daughter-in-law's is overdue to have her baby and we had a couple of days running back and forth from the hospital with false alarms and I completely forgot.

I think it looks neater in the Stats block. . . . not sure it would be classed as a stat, mind you. But then, I don't really understand the necessity of it being there in the first place :)
 
The date and time wouldn't be classed as statistics, which is a collection of data (in this case from your forum).

I think that date and time in the stats block is completely lost in there. Presumably you're wanting to show users what the date and time are? I doubt many will notice it there.

Also your date format, whist OK for those in the US, Canada and a couple of other countries, sucks for the rest of the world :p

https://en.m.wikipedia.org/wiki/Date_format_by_country

Personally I wouldn't bother with the date and time on the forums, they're already displayed on the devices that your users are accessing the site with. :)
 
Last edited:
Top Bottom