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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

$date.nice


Go to End


7 Posts   17578 Views

Avatar
FireMe!

Community Member, 74 Posts

9 March 2009 at 5:30am

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

Community Member, 181 Posts

10 March 2009 at 9:10am

Edited: 27/03/2009 7:13am

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

Avatar
svakanda

Community Member, 1 Post

10 March 2009 at 10:10pm

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

Community Member, 181 Posts

11 March 2009 at 2:11am

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!

Community Member, 74 Posts

11 March 2009 at 7:20am

Edited: 11/03/2009 7:22am

<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

Community Member, 181 Posts

11 March 2009 at 7:30am

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

Avatar
Vegas Rob

Community Member, 5 Posts

8 May 2010 at 4:17am

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"