試試這個
**`Set the default timezone`**
您可以設置默認的時區以兩種方式:讓我們假設「歐洲/巴黎」就是你要設置時區:
Open your **`php.ini`** file in your favourite text editor and change the following line: **`date.timezone = Europe/Paris.`** You may have to restart your server in order to make this effective.
In case you don't want to meddle with your ini file you can simply set the default time zone using the following PHP function:
<?php date_default_timezone_set('Europe/Paris') ?>
List of PHP supported Timezones 更新您的時區
PHP安裝使用上PECL可用timezonedb包。
您可以查看當前版本的timezonedb用:
<?php echo timezone_version_get() ?>
你會看到這樣2012.9至極了一些對應的日期。有時時區會更新,如果套餐年齡超過一年,我建議您更新您的timezonedb。您可以通過在終端上執行以下命令來安裝/更新timezonedb版本:
**$ sudo pecl install timezonedb**
不要忘了,爲了使這一有效增加新的擴展爲php.ini,然後重新啓動服務器。
extension=timezonedb.so
* Get the User Timezone
* 讓用戶選擇它
與您已通過大陸上市的所有時區的上述功能。然後,您可以爲各個國家和地區創建一個下拉菜單。
<?php
function timezone_options()
{
$zones = array(
'Africa', 'America', 'Antarctica', 'Arctic', 'Asia',
'Atlantatic', 'Australia', 'Europe', 'Indian', 'Pacific'
);
$list = timezone_identifiers_list();
foreach ($list as $zone) {
list($zone, $country) = explode('/', $zone);
if (in_array($zone, $zones) && isset($country) != '') {
$countryStr = str_replace('_', ' ', $country);
$locations[$zone][$zone.'/'.$country] = $countryStr;
}
}
return $locations;
}
Get it via Javascript and PHP
Time zone are written as offset from UTC in the format **±[hh]:[mm], ±[hh][mm], or ±[hh].** So if the time being described is one hour ahead of UTC (such as the time in Berlin during the winter), the zone designator would be "**+01:00", "+0100", or simply "+01".**
通過PHP你不能直接訪問用戶時區。要獲得用戶時區,您必須獲得他的偏移量並瞭解是否遵守節能日光。您可以使用JavaScript來獲取這樣的信息:
function get_timezone_infos() {
var now = new Date();
var jan1 = new Date(now.getFullYear(), 0, 1, 0, 0, 0, 0);
var temp = jan1.toGMTString();
var jan2 = new Date(temp.substring(0, temp.lastIndexOf(' ') - 1));
var offset = (jan1 - jan2)/(1000);
var june1 = new Date(now.getFullYear(), 6, 1, 0, 0, 0, 0);
temp = june1.toGMTString();
var june2 = new Date(temp.substring(0, temp.lastIndexOf(' ') - 1));
var dst = offset != ((june1 - june2)/(1000));
return { offset: offset, dst: dst };
}
數據庫存儲
首先最,這不是真的很難存儲在數據庫中的日期。我最喜歡的選擇是MySQL datetime字段,您可以在其中一個字段中存儲日期和時間。 對於DateTime類型的所有列,必須以UTC時區存儲DateTime。
設置日期時間時區爲UTC將其存儲在數據庫之前:
<?php
function toDatabase($dateTime)
{
$dateTime->setTimezone(new DateTimeZone('GMT'));
return $dateTime;
}
當檢索日期時間從數據庫中把它設置爲原來的時區:
<?php
// set the timezone of the current user before calling the function
date_default_timezone_set('Europe/Paris');
<?php
function toUser($dateTime)
{
$dateTime->setTimezone(new DateTimeZone(date_default_timezone_get()));
return $dateTime;
}
我不確定我瞭解問題所在。您是否在解析表單輸入並將其提供給'get_timezone_offset'函數時遇到問題? – Technoh
你明確地給你的函數賦值。正如你把歐洲對亞洲,你會收到-7。如果你想把表格放入它,你應該通過$ _POST ['timezone1']和2作爲論據 –
@Technoh,是的,這是正確的。基本上我想做的是去掉'歐洲/倫敦','亞洲/上海'這兩個時區1和時區2,並讓用戶使用HTML格式提供的表單搜索位置。 –