Joomla2.5以後,時間類 JDate 繼承的是php(5.3及以上)的基礎類庫:DateTime
首先在後臺將時區設置爲 當前時區:
系統=》全局設置=》服務器=》服務器時區=》Shanghai
也可直接修改:configuration.php 中的 $offset
joomla 裏獲取當前時間:
//獲取當前時間數組
$n1 = new JDate('now', new DateTimeZone(JFactory::getConfig()->get('offset')));
//或:
$n1 = JFactory::getDate('now', JFactory::getConfig()->get('offset'))
//獲取當前時間
$n1->toSql();
用生成工具:component-creator 生成的代碼,時間是這樣獲取的:
JFactory::getDate()->toSql();
但是,新建文章後,時間並不是當前時間,而是UTC時間。
若不想修改生成的代碼,可以嘗試修改date類。
找到:libraries/joomla/date/date.php 文件 大概在86行:
將:
self::$gmt = new DateTimeZone('GMT');
修改爲:
self::$gmt = new DateTimeZone('Asia/Shanghai');