Custom Date & Time Formats

Hi,

I’m a Yii newbie so I suspect this will be simple oversight on my part but after spending some considerable time looking for the solution via Google I’m still unable to get my code working.

The problem :

I’ve like to add a additional custom date & time format to show dates as Dayname dd/MM/yyyy

My attempt :

Following the instructions at Yiiplayground.com > I18n and I10n > Basic date and time (Due to thie being my first post I can’t post URLs - sorry!) I’ve created the following file protected/i18n/data/en_gb.php with the following contents.


<?php

/**

 * Extends Locale data for 'en_GB'.

 * In this file you can put custom locale settings that will be

 * merged with the ones provided by the framework

 * ( that are stored in <framework_dir>/i18n/data/ )

 */


  error_log(sprintf("%s (%s): %s", __FILE__, __LINE__, "In extended en_gb.php"));

return CMap::mergeArray(

  require(dirname($GLOBALS['yii']).'/i18n/data/'.basename(__FILE__)),

  array(

    'dateFormats' => array(

      'small'=>'ddd dd/MM/yyyy',          // format used for input

      'calendar_small'=>'ddd dd/mm/yy',   // format used for input with calendar widget

      'database'=>Yii::app()->params['database_format']['date'],

    ),

    'timeFormats' => array(

      'small'=>'HH:mm:ss',          // format used for input

      'database'=>Yii::app()->params['database_format']['time'],

    )

  )

);

?>

The protected/components/LocaleManager.php file is as described on Yii Playground.

The following is set in my index.php


//Yii::createWebApplication($config)->run();

$app = Yii::createWebApplication($config);

Yii::app()->setLanguage('en_gb');

Yii::app()->setTimezone('Europe/London');

$app->run();

echoing Yii::app()->getLanguage(); returns en_gb which would indicate (to me at least) that the files is correctly named at en_gb.php

However, the following code breaks at line 9 …


<ul>

<li>Current language: <?php echo Yii::app()->getLanguage(); ?></li>

<li>Date &amp; time short: <?php echo Yii::app()->dateFormatter->formatDateTime(time(), 'short'); ?></li>

<li>Date medium: <?php echo Yii::app()->dateFormatter->formatDateTime(time(), 'medium', false); ?></li>

<li>Time medium: <?php echo Yii::app()->dateFormatter->formatDateTime(time(), false, 'medium'); ?></li>

<li>Date short format: <?php echo Yii::app()->locale->getDateFormat('short'); ?></li>

<li>Time medium format: <?php echo Yii::app()->locale->getTimeFormat('medium'); ?></li>

<li>Parsed date and time: <?php echo Yii::app()->dateFormatter->format(Yii::app()->locale->getDateFormat('long'), CDateTimeParser::parse('04/06/2010', 'dd/MM/yyyy')); ?></li>

<li>Date &amp; time custom format: <?php echo Yii::app()->dateFormatter->formatDateTime(time(), 'small', 'small'); ?></li>

</ul>

… with the following error

As you can see from my first block of code I’ve stuck a…


error_log(sprintf("%s (%s): %s", __FILE__, __LINE__, "In extended en_gb.php"));

… in to see if the script is ever executed, it isn’t.

I’ve put similar error_log() code in LocaleManager.php which also isn’t executed unless I preload the script with the following in my config/main.php…




return array(

  //'theme'=>'abound',

  'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',

  'name'=>'JMS v3.1',


  // preloading 'log' component

  'preload'=>array('log', 'lm'),

  ..

  ..

    'components'=>array(

    ..

        'lm'=>array(

      'class'=>'LocaleManager'

     ),

    ..

  )



Even when LocaleManager.php is executed with preloading enabled it doesn’t resolve my problem, I still get the error

Any help gratefully received!

Steve

I resolved my issue. I needed to set localeDataPath in my main.php


return array(

  ..

  // autoloading model and component classes

  'import'=>array(

    'application.models.*',

    'application.components.*',

  ),

  'localeDataPath'=>'protected/i18n/data/',

  ..

);

cheers

Steve