在即將推出的CodeIgniter版本中,我們將介紹一個非常棒的配置選項來設置時區。目前我們只能在「time_reference」選項中設置「local」或「gmt」。但在即將推出的版本中,我們將能夠使用「time_reference」,如「亞洲/達卡」或「澳大利亞/悉尼」。這意味着我們將能夠使用PHP支持的所有時區。但是如果我們想在舊版本的CodeIgniter中使用這個功能,我們需要做什麼。當然,有幾種方法。但是今天我想分享一些簡單的技巧在CodeIgniter舊版本中使用時區(< 3.0-dev版本)。
要做到這一點,我們需要遵循以下步驟:
=>需要用下面的函數延長日期幫手。
if (! function_exists('now'))
{
/**
* Get "now" time
*
* Returns time() based on the timezone parameter or on the
* "time_reference" setting
*
* @param string
* @return int
*/
function now($timezone = NULL)
{
if (empty($timezone))
{
$timezone = config_item('time_reference');
}
if ($timezone === 'local' OR $timezone === date_default_timezone_get())
{
return time();
}
$datetime = new DateTime('now', new DateTimeZone($timezone));
sscanf($datetime->format('j-n-Y G:i:s'), '%d-%d-%d %d:%d:%d', $day, $month, $year, $hour, $minute, $second);
return mktime($hour, $minute, $second, $month, $day, $year);
}
}
=>現在,在 「time_reference」 選項的config.php我們可以用這樣的時區:
$config['time_reference'] = 'Asia/Dhaka';
要知道更多:https://www.linkedin.com/groups/109912/109912-5953416141937405955
'date_default_timezone_set( '亞洲/加爾各答' );'在插入查詢之前添加它。最好在主index.php的開頭添加這個,以便在每個查詢之前不需要寫入。 –