1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

XF 1.5 Custom Sidebar Block with Current Date and Time

Discussion in 'Styling and Customization Questions' started by Amaury, Feb 2, 2016.

  1. Amaury

    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.PNG

    Custom Sidebar 2.PNG

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

    Custom Sidebar 3.PNG

    What am I doing wrong? Thanks.

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

    Custom Sidebar 4.png
     
  2. Brogan

    Brogan XenForo Moderator Staff Member

    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.
     
    Amaury likes this.
  3. katsulynx

    katsulynx Well-Known Member

    Phrases are text only unless you hand them over a variable as additional parameter when you call them.
     
    Amaury likes this.
  4. Lisa

    Lisa Well-Known Member

    Code:
    <h3>Current Time and Date</h3>
    {xen:datetime $serverTime, 'absolute'}
    datetime.png

    Something like that?
     
    Amaury likes this.
  5. Amaury

    Amaury Well-Known Member

    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.
     
  6. Lisa

    Lisa Well-Known Member

    Is that Month, Day, Year you have?

    datetime2.png

    Code:
    {xen:date $serverTime, 'm/d/y *  g:i A'}
     
    Amaury likes this.
  7. Amaury

    Amaury Well-Known Member

    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'}
     
  8. Lisa

    Lisa Well-Known Member

    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?
     
    Amaury likes this.
  9. SneakyDave

    SneakyDave Well-Known Member

    Amaury and Lisa like this.
  10. Lisa

    Lisa Well-Known Member

    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.png
    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 ;)
     
    Amaury likes this.
  11. Amaury

    Amaury Well-Known Member

    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! :)
     
    Lisa likes this.
  12. Lisa

    Lisa Well-Known Member

    Personally, I wouldn't bother - but I'm lazy like that. If I'm already in the template I'd just put there lol

    And you're welcome :)
     
    Amaury likes this.
  13. Amaury

    Amaury Well-Known Member

    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.
     
    Lisa likes this.
  14. katsulynx

    katsulynx Well-Known Member

    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.
     
  15. Amaury

    Amaury Well-Known Member

    Sorry, @Lisa. I lied. One last question. This isn't as important, but is it possible to add a GMT indicator?

    Current Date and Time.PNG
     
  16. Lisa

    Lisa Well-Known Member

    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 :)
     
    Amaury likes this.
  17. Amaury

    Amaury Well-Known Member

    @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.PNG

    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.PNG

    Under the time would be the time zone (GMT -8 or -7 in my case).
     
  18. Lisa

    Lisa Well-Known Member

    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 :)
     
    Amaury likes this.
  19. Amaury

    Amaury Well-Known Member

    No worries, Lisa. When you get the chance.
     
  20. Martok

    Martok Well-Known Member

    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: Feb 6, 2016
    Amaury and Lisa like this.

Share This Page