Jump to:

3444 Posts in 1030 Topics by 871 members

Template Questions

SilverStripe Forums » Template Questions » A solution to getting your date and time in your locale

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

Page: 1
Go to End
Author Topic: 380 Views
  • yurigoul
    Avatar
    Community Member
    202 Posts

    A solution to getting your date and time in your locale Link to this post

    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.

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