2013-08-21 3 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, 맞습니다. 기본적으로 내가하고 싶은 것은 timezone1과 timezone2 인 'Europe/London', 'Asia/Shanghai'를 제거하고 사용자가 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 패키지가 사용됩니다. 당신이 2012.9 느릅 나무 같은 숫자가 날짜에 해당 볼

<?php echo timezone_version_get() ?> 

:

당신은 귀하의 timezonedb의 현재 버전을 확인할 수 있습니다. 때로는 시간대가 업데이트되어 패키지가 1 년 이상 지난 경우 시간대를 업데이트하는 것이 좋습니다. 당신은 터미널에서 다음 명령을 실행하여 timezonedb 버전을 설치/업데이트 할 수 있습니다

**$ sudo pecl install timezonedb** 

이 유효하게하기 위해 서버를 php.ini 파일에 새 확장을 추가 한 다음 다시 시작하는 것을 잊지 마십시오. * Get the User Timezone *

extension=timezonedb.so 는 사용자가이 대륙에 의해 나열된 모든 시간대가 위의 기능과 함께 그것을

을 선택하자. 그런 다음 대륙에 대한 드롭 다운 메뉴와 각 국가 별 옵션을 만들 수 있습니다.

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를 통해 사용자의 시간대에 액세스 할 수 없습니다

<?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; 
} 
. 사용자 시간대를 얻으려면 오프셋을 가져와 일광 절약 시간을 확인해야합니다. 이 정보를 얻기 위해 자바 스크립트를 사용할 수 있습니다

가장 먼저, 데이터베이스에 저장 날짜에 정말 어렵지 않다

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 }; 
} 

데이터베이스 저장. 필자가 선호하는 것은 dateTime을 하나의 필드에 저장할 수있는 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를 호출하여 사용자가 원하는대로 시간을 변경할 수 있기 때문에 (적어도 일부 상황에서는) 잘못되었습니다. 또한 google . –

0

당신은 쉽게 제거 할 수 입력하고 사용 가능한 모든 시간대의 드롭 다운 목록으로 대체하십시오. PHP가 지원하는 시간대 목록은 official PHP documentation에서 확인할 수 있습니다. 그런 다음 한 목록의 값과 다른 목록의 값을 직접 비교할 수 있습니다.

또한 Generating a drop down list of timezones with PHP을 참조하십시오. 누군가이 목록을 작성했습니다.

DateTimeZone::listIdentifiers(DateTimeZone::ALL); 또한 사용 가능한 시간대 목록을 만듭니다.

+0

잘 그게 내 문제입니다. 드롭 다운 목록을 만드는 것은 내가 따라야 할 다음 단계 였지만 순간에는 입력 양식이나 드롭 다운 목록에서 "값을 비교할"수있는 방법을 알아야합니다. –