Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Reply

7 Posts   10759 Views

Avatar
FireMe!

9 March 2009 at 5:30am Community Member, 74 Posts

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!

Avatar
Ben Gribaudo

10 March 2009 at 9:10am (Last edited: 27 March 2009 7:13am), Community Member, 181 Posts

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 [url=http://silverstripe.org/template-questions/show/254953?start=0#post255626#post255626]will change[/url]. :-)

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

Ben

Avatar
svakanda

10 March 2009 at 10:10pm Community Member, 1 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!

Avatar
Ben Gribaudo

11 March 2009 at 2:11am Community Member, 181 Posts

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

Avatar
FireMe!

11 March 2009 at 7:20am (Last edited: 11 March 2009 7:22am), Community Member, 74 Posts

<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

Avatar
Ben Gribaudo

11 March 2009 at 7:30am Community Member, 181 Posts

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

Avatar
Vegas Rob

8 May 2010 at 4:17am Community Member, 5 Posts

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"