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.

Template Questions

A solution to getting your date and time in your locale


Reply

395 Views

Avatar
yurigoul

2 August 2013 at 3:38am Community Member, 202 Posts

My example is based on German and Silverstripe 3.

When you have a datefield in your cms that you use to set a date and you want to set the date in a locale different from en_US. In your controller add the function:

function localDate($myDate) {
   $newLocale = setlocale(LC_TIME, 'de_DE', 'de_DE.UTF-8');
   return strftime('%d. %B %Y', strtotime($myDate));
}

In your template use $localDate($myDateField) and you are golden.

Change the de_DE to your own locale. Change '%d. %B %Y' to something that looks nice to you: http://www.php.net/manual/en/function.date.php

When getting the time from a DateTimeField use $myDateTime.Time24() in your templates together with the localDate() function because there seems to be a problem with getting the minutes.