Jump to:

23483 Posts in 18976 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » dateformat full month YYYY

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

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

Page: 1
Go to End
Author Topic: 2170 Views
  • ambient
    Avatar
    Community Member
    123 Posts

    dateformat full month YYYY Link to this post

    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

  • Invader_Zim
    Avatar
    Community Member
    141 Posts

    Re: dateformat full month YYYY Link to this post

    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

  • ambient
    Avatar
    Community Member
    123 Posts

    Re: dateformat full month YYYY Link to this post

    Christian you are a scholar and a gent

    That worked perfect, thank you so much

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: dateformat full month YYYY Link to this post

    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();
    }

  • benni91
    Avatar
    Community Member
    72 Posts

    Re: dateformat full month YYYY Link to this post

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

    Thanks Benni

  • ambient
    Avatar
    Community Member
    123 Posts

    Re: dateformat full month YYYY Link to this post

    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

  • benni91
    Avatar
    Community Member
    72 Posts

    Re: dateformat full month YYYY Link to this post

    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

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