2013-08-21 16 views
-1

我正在嘗試創建一個簡單的應用程序,允許用戶搜索兩個位置,並向他們顯示兩個位置的時差。PHP中的時差?

我可以使用代碼下面的代碼獲得兩個位置的偏移量,但是我無法將它連接到表單輸入,因此用戶可以選擇位置!

這是我迄今所做的:

<form id="form1" name="form1" method="post" action="<?php $get_timezone_offset ?>"> 
    <label> 
    <input type="text" class="timezone1" id="timezone1" name="timezone1" id="timezone1" /> 
    <input type="text" class="timezone2" id="timezone2" name="timezone2" id="timezone2" /> 
    <input name="" type="button" value="submit" /> 
    </label> 
</form> 

    <?php 
    function get_timezone_offset($origin_tz, $remote_tz) { 
     $timezone1 = new DateTimeZone($origin_tz); 
     $timezone2 = new DateTimeZone($remote_tz); 

     $datetime1 = new DateTime("now", $timezone1); 
     $datetime2 = new DateTime("now", $timezone2); 

     $offset = $timezone1->getOffset($datetime1) - $timezone2->getOffset($datetime2); 
     return $offset; 
    } 

    $offset = get_timezone_offset('Europe/London', 'Asia/Shanghai'); 

    // convert offset to hours 
    echo $offset/3600; 
    ?> 

任何幫助,將不勝感激。

+0

我不確定我瞭解問題所在。您是否在解析表單輸入並將其提供給'get_timezone_offset'函數時遇到問題? – Technoh

+0

你明確地給你的函數賦值。正如你把歐洲對亞洲,你會收到-7。如果你想把表格放入它,你應該通過$ _POST ['timezone1']和2作爲論據 –

+0

@Technoh,是的,這是正確的。基本上我想做的是去掉'歐洲/倫敦','亞洲/上海'這兩個時區1和時區2,並讓用戶使用HTML格式提供的表單搜索位置。 –

回答

0

試試這個

**`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; 
} 
+0

我無權訪問ini文件,因爲它是共享的測試服務器,不允許編輯.ini文件。我遵循你的代碼,部分不會做任何事情,部分會在頁面上拋出錯誤。所以很確定答案是無效的。即使你花時間寫了它。 –

+0

看到這個http://stackoverflow.com/questions/4746249/get-user-timezone – krishna

+0

我不知道我不得不在該頁面上看到什麼,但如果你指的是第一個答案,那麼下面是這樣的:我認爲這個解決方案從根本上說是錯誤的(至少在某些情況下),因爲它允許用戶通過調用domain.com/timezone.php來改變時間,無論他想要什麼,還有,我發現在谷歌。 –

0

你可以輕鬆去除輸入並將其替換爲所有可用時區的下拉列表。 PHP支持的時區列表可在official PHP documentation中找到。然後你可以直接比較從一個列表到另一個列表的值。

此外,請參閱Generating a drop down list of timezones with PHP,其中有人爲您提供該列表。

DateTimeZone::listIdentifiers(DateTimeZone::ALL);也將創建可用時區列表。

+0

那是我的問題。創建一個下拉列表是我將要關注的下一個階段,但目前我需要知道如何在輸入表單或下拉列表中「比較值」! –