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.

Customising the CMS /

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

Date formating


Go to End


1241 Views

Avatar
Pike

Community Member, 42 Posts

15 January 2010 at 4:28am

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