Jump to:

5541 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Date formating

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

Page: 1
Go to End
Author Topic: 752 Views
  • Pike
    Community Member
    40 Posts

    Date formating Link to this post

    Hi to all,

    Methods like .Nice, .Nice24, Format, FormatI18N is problematic for me.

    Becouse e.g.:
    1.   %e parameter not works under OS Windows
    2.   I’m Czech and results from strftime() I must convert to utf8:

    $MyDate = strftime( $date_formats[$date_formats_item], strtotime( $this->value ) );
    if ( !$ut8encode ) return $MyDate;
    return ( strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN' ? iconv("CP1250", "UTF-8", $MyDate ) : utf8_encode( $MyDate ) );

    So I arbitrated for custom solution:

    I’ve global variable $date_formats in mysite/_config

    $locale_lang = i18n::get_lang_from_locale(i18n::get_locale());
    if ( preg_match("/cs|sk|de/i", $locale_lang ) ) {
    $date_formats    = array(
       'date_time_iso'   => '%Y-%m-%d %H:%i:%s',
       'date_time'   => '%e.%c.%Y %H:%i:%s',
       'date_hm'   => '%e.%c.%Y %H:%i',
       'date_named_full'=> '%W, %e. %M %Y %H:%i:%s',
       'date_named'   => '%W, %e. %N %Y',
       'date'      => '%e.%c.%Y',
       'time'      => '%H:%i:%s',
       'hm'      => '%H:%i'
    else {
       $date_formats    = array(
       'date_time_iso'   => '%Y-%m-%d %H:%i:%S',
       'date_time'   => '%m.%d.%Y %h:%i:%s',
       'date_hm'   => '%m.%d.%Y %h:%i %r',
       'date_named_full'=> '%W, %M %e, %Y %h:%i %r',
       'date_named'   => '%W, %M %e, %Y',
       'date'      => '%M %e, %Y',
       'time'      => '%h:%i:%s %r',
       'hm'      => '%h:%i %r'

    Then I can call this method:

    public function MyDateFormat( $date_formats_item, $ut8encode = true ) {
    global $date_formats;

       $locale_lang   = i18n::get_locale();
       $ls_time_names= DB::query( "SET lc_time_names='$locale_lang'");
       $MyDate    = DB::query( "SELECT DATE_FORMAT( '$this->value', '$date_formats[$date_formats_item]' )")->value();
       return $MyDate;

    My problem is, that I must add this method to Date class, SSDDatetime class and Time class.
    My question is:
    Where can I put my function MyDateFormat to be visible for all 3 classes?
    Or how can I extend all 3 classes by my method MyDateFormat in the one place?
    Help me, please……..

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.