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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

dateformat full month YYYY


Go to End


7 Posts   8520 Views

Avatar
ambient

Community Member, 130 Posts

17 June 2011 at 5:03am

Hi all,

I thought this would be an easy thing to do but after banging my head against the wall till it bled I now have no choice but to seek your help.

I want the dates from the 2nd tutorial to display as e.g. "April 2010" instead of 04 2010

I thought it would be as easy as changing MM YYYY to something different in this line

$dateField->setConfig('dateformat', 'MM YYYY');

but haven't been able to figure it out.

Can this be done? I'm not very experienced as a developer so I hope someone might have a solution :)

Thanks for looking

Avatar
Invader_Zim

Community Member, 141 Posts

17 June 2011 at 7:11am

Hi ambient.

If your problem is just the output, you could try this in your template (themes/tutorial/templates/Layout/ArticlePage.ss):
Instead of this

...
    $Content
    <div class="newsDetails">
        Posted on $Date.Nice by $Author
    </div>
...

try this:

...
    $Content
    <div class="newsDetails">
        Posted on $Date.Month  $Date.Year by $Author
    </div>
...

For more methods have a look at the Date Class in /sapphire/core/model/fieldtypes/Date.php

Cheers
Christian

Avatar
ambient

Community Member, 130 Posts

18 June 2011 at 12:00am

Christian you are a scholar and a gent :)

That worked perfect, thank you so much

Avatar
martimiz

Forum Moderator, 1391 Posts

19 June 2011 at 11:31pm

Dates are confusing :-) $dateField->setConfig() is used when setting the default format for display in a Date field within a form as in

$dateField = new DateField( 'MyDate', 'Soeme date');
$dateField->setConfig('dateformat', 'MM yyyy');

To format a date for output in your Template, Christians post of course is the way to go :-)

If for some reason you wanted to format a date from within your Page controller, you'd could create a Date object first, then use its methods. Something like this:

function getMyDateNice() {
return $this->obj('MyDate')->Nice();
}

Avatar
benni91

Community Member, 72 Posts

11 July 2011 at 4:50am

Hi,
can you tell me how to use this function to get an date output like this
DD. Month YYYY.

Thanks Benni

Avatar
ambient

Community Member, 130 Posts

12 July 2011 at 12:08am

Hi Benni,

I didn't use the function in the end as I only wanted the date to output a certain way.

If thats all you want to then I would suggest using Christians technique.

In your ss file something like

 $Date.DayOfMonth $Date.Month $Date.Year

You can see more methods of the Date Class in /sapphire/core/model/fieldtypes/Date.php

Avatar
benni91

Community Member, 72 Posts

12 July 2011 at 3:31am

Hi,

i looked up the code in "/sapphire/core/model/fieldtypes/Date.php"
edit line 47 like this

function Nice() {
if($this->value) return date('d. F Y', strtotime($this->value));
}

and everything works fine :)

thx ambient