Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » $date.nice

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 10038 Views
  • FireMe!
    Avatar
    Community Member
    74 Posts

    $date.nice Link to this post

    Hi,

    This is probably something really simple to do, but i am not so good at php yet $date.nice outputs as "17/02/09" but what i want to do is output it separately. like day of the month as a number, month as 3 letter word. Dont really need the year so the example above would be "17 Feb" and i want to be able to add separate css to them.

    Thanks in advance

    FireMe!

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: $date.nice Link to this post

    Hi,

    Does $date.format(%e %b) help?

    Or, if you want the date and month to be styled differently, you could do:

    <span class="day">$date.format(%e)</span> <span class="month">$date.format(%b)</span>

    You can find a list of valid date formatting codes on http://us.php.net/strftime. At present, you can't use commas inside the call to format, however, I'm hoping that will change.

    Edit: Actually, the formatting codes may be found at: http://us2.php.net/date

    Ben

  • svakanda
    Avatar
    Community Member
    1 Post

    Re: $date.nice Link to this post

    i am really missing something. I can't figure out where those special functions are defined....like if i try and use $date.format(%e %b) in any of my Layout templates i just error out.
    :Fatal error: Call to a member function XML_val() on a non-object:

    i can't figure out how to use any php...after lots of reading i guess it needs to be adding to the page controller as a function, but i don't understand things like $date, or how to access it...can anyone shed some light? thanks!

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: $date.nice Link to this post

    Hi,

    Have you seen http://doc.silverstripe.org/doku.php?id=date&s=format? Is $date defined as a property in your page's model (e.g. in the static $db array)?

    Yes, you are correct that PHP code needs to be in the controller (or model), not in the template. However, ".format" on a date property should work in a template.

    Ben

  • FireMe!
    Avatar
    Community Member
    74 Posts

    Re: $date.nice Link to this post

    <span class="day">$date.format(%e)</span> <span class="month">$date.format(%b)</span>

    didnt work for me for some reason, its displayed as %Europe/London %b
    but i changed it to.

    <span class="day">$date.format(d)</span> <span class="month">$date.format(m)</span>

    and that displayed as 17 Feb

    anyways thanks for your help, your code layout helped out alot,i am not to good at php/mysql

    FireMe

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: $date.nice Link to this post

    Sure thing. Sorry about those extra percent signs. I forgot that they are left off when in a template.

  • Vegas Rob
    Avatar
    Community Member
    5 Posts

    Re: $date.nice Link to this post

    Thanks for the help here. Saved me some time last night. Just a note:

    That link to the docs doesn't seem to be working (anymore?). After I already solved my issue, I finally found what I was looking for in the docs.

    http://api.silverstripe.org/2.4/sapphire/model/Date.html

    It's a big mess to read, but with functionality you are "sure" is there, it can be very helpful.

    Also, in plain ol' php you can easily add extra text to the formatting. Within SilverStripe however, adding commas or other symbols can cause issues. It was shown by others but I had to format like this: $Date.format(l), $Date.format(M d), $Date.format(Y) to get "Thursday, May 13, 2010"

    10038 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.