Last active
October 3, 2015 06:58
-
-
Save jamband/2412321 to your computer and use it in GitHub Desktop.
Yii Framework: Extending CFormatter sample.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$time = 1334755524; | |
/** | |
* Yii::app()->languageが ja の場合 | |
*/ | |
echo Yii::app()->dateFormatter->formatDatetime($time)."<br />\n"; | |
// 2012/04/1822:25:24 | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'full')."<br />\n"; | |
// 2012年4月18日水曜日22:25:24 | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'long')."<br />\n"; | |
// 2012年4月18日22:25:24 | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'medium')."<br />\n"; | |
// 2012/04/1822:25:24 | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'short')."<br />\n"; | |
// 12/04/1822:25:24 | |
echo Yii::app()->dateFormatter->formatDatetime($time, null)."<br />\n"; | |
// 22:25:24 | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'full', 'full')."<br />\n"; | |
// 2012年4月18日水曜日22時25分24秒 JST | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'full', 'long')."<br />\n"; | |
// 2012年4月18日水曜日22:25:24 JST | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'full', 'medium')."<br />\n"; | |
// 2012年4月18日水曜日22:25:24 | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'full', 'short')."<br />\n"; | |
// 2012年4月18日水曜日22:25 | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'full', null)."<br />\n"; | |
// 2012年4月18日水曜日 | |
echo "<hr />\n"; | |
/** | |
* Yii::app()->languageが en の場合 | |
*/ | |
Yii::app()->setLanguage('en'); | |
echo Yii::app()->dateFormatter->formatDatetime($time)."<br />\n"; | |
// Apr 18, 2012 10:25:24 PM | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'full')."<br />\n"; | |
// Wednesday, April 18, 2012 10:25:24 PM | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'long')."<br />\n"; | |
// April 18, 2012 10:25:24 PM | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'medium')."<br />\n"; | |
// Apr 18, 2012 10:25:24 PM | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'short')."<br />\n"; | |
// 4/18/12 10:25:24 PM | |
echo Yii::app()->dateFormatter->formatDatetime($time, null)."<br />\n"; | |
// 10:25:24 PM | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'full', 'full')."<br />\n"; | |
// Wednesday, April 18, 2012 10:25:24 PM JST | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'full', 'long')."<br />\n"; | |
// Wednesday, April 18, 2012 10:25:24 PM JST | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'full', 'medium')."<br />\n"; | |
// Wednesday, April 18, 2012 10:25:24 PM | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'full', 'short')."<br />\n"; | |
// Wednesday, April 18, 2012 10:25 PM | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'full', null)."<br />\n"; | |
// Wednesday, April 18, 2012 | |
echo "<hr />\n"; | |
/** | |
* Yii::app()->languageが zh の場合 | |
*/ | |
Yii::app()->setLanguage('zh'); | |
echo Yii::app()->dateFormatter->formatDatetime($time)."<br />\n"; | |
// 2012-4-18下午10:25:24 | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'full')."<br />\n"; | |
// 2012年4月18日星期三下午10:25:24 | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'long')."<br />\n"; | |
// 2012年4月18日下午10:25:24 | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'medium')."<br />\n"; | |
// 2012-4-18下午10:25:24 | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'short')."<br />\n"; | |
// 12-4-18下午10:25:24 | |
echo Yii::app()->dateFormatter->formatDatetime($time, null)."<br />\n"; | |
// 下午10:25:24 | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'full', 'full')."<br />\n"; | |
// 2012年4月18日星期三JST下午10时25分24秒 | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'full', 'long')."<br />\n"; | |
// 2012年4月18日星期三JST下午10时25分24秒 | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'full', 'medium')."<br />\n"; | |
// 2012年4月18日星期三下午10:25:24 | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'full', 'short')."<br />\n"; | |
// 2012年4月18日星期三下午10:25 | |
echo Yii::app()->dateFormatter->formatDatetime($time, 'full', null)."<br />\n"; | |
// 2012年4月18日星期三 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return array ( | |
... | |
'weekDayNames' => | |
array ( | |
'wide' => | |
array ( | |
// 0 => '日曜日', | |
// 1 => '月曜日', | |
// 2 => '火曜日', | |
// 3 => '水曜日', | |
// 4 => '木曜日', | |
// 5 => '金曜日', | |
// 6 => '土曜日', | |
0 => '(日)', | |
1 => '(月)', | |
2 => '(火)', | |
3 => '(水)', | |
4 => '(木)', | |
5 => '(金)', | |
6 => '(土)', | |
), | |
... | |
... | |
'dateFormats' => | |
array ( | |
// 'full' => 'y年M月d日EEEE', | |
'full' => 'y年MM月dd日', | |
'long' => 'y年M月d日', | |
'medium' => 'yyyy/MM/dd', | |
'short' => 'yy/MM/dd', | |
), | |
'timeFormats' => | |
array ( | |
'full' => 'H時mm分ss秒 zzzz', | |
'long' => 'H:mm:ss z', | |
'medium' => 'H:mm:ss', | |
// 'short' => 'H:mm', | |
'short' => 'HH時mm分', | |
), | |
// 'dateTimeFormat' => '{1}{0}', | |
'dateTimeFormat' => '{1} {0}', | |
'amName' => '午前', | |
'pmName' => '午後', | |
'orientation' => 'ltr', | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 1. /protected/components/MyFormatter.php を作成。CFormatter を extends する | |
* 2. format から始まる名前で独自のメソッドを追加 | |
*/ | |
class MyFormatter extends CFormatter | |
{ | |
public function formatBold($val) | |
{ | |
return '<b>' . $val . '</b>'; | |
} | |
} | |
?> | |
<?php | |
/** | |
* 1. どこかのビューにて Yii::app()->format->formatName('値') を echo する | |
* ( formatName 箇所は MyFormatter のメソッド名の format を抜いて頭を小文字にしたもの ) | |
* 2. 文字が太くなったか確認 | |
*/ | |
echo Yii::app()->format->bold('Yii Framework'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 1. /protected/views/book/view.php の任意のカラムに :bold を付け足す | |
* 2. title カラムが太い文字になっているか確認 | |
*/ | |
?> | |
<?php $this->widget('zii.widgets.CDetailView', array( | |
'data' => $model, | |
'attributes' => array( | |
'id', | |
'title:bold', | |
... | |
), | |
)); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment