PHP nasıl Greenwich Mean Time alabilirim?

5 Cevap php

Ben EST için ayarlanmış bir sunucusu var, ve veritabanındaki tüm kayıtları EST ayarlanır. Ben GMT olarak ayarlamak için nasıl bilmek istiyorum. Benim kullanıcılara bir zaman dilimi seçeneği sunmak istiyoruz.

5 Cevap

Olursa olsun sunucu burada GMT zaman dilimi herhangi bir zaman dilimi için zaman ve tarih almak son derece kolay bir şekilde olduğu olduğunu. Bu time() ve gmdate() fonksiyonu ile yapılır. gmdate() function normalde biz GMT + N veya GMT-N herhangi GMT zaman dilimi için zaman almak anlamına gelir alabilirsiniz zaman () fonksiyonu ile bir hile yaparak bize GMT zaman ver ama.

Örneğin GMT +5 biz aşağıdaki gibi bir bunu yapacağız zaman almak zorunda

<?php 
  $offset=5*60*60; //converting 5 hours to seconds.
  $dateFormat="d-m-Y H:i";
  $timeNdate=gmdate($dateFormat, time()+$offset);
?>

Eğer GMT-5 olduğu zaman almak zorunda Şimdi eğer şimdi biz sadece yerine biz GMT-4 için zaman alıyorsanız örnek aşağıdaki gibi bir zaman içine ilave zaman () ofset çıkarma yapacak

<?php 
  $offset=4*60*60; //converting 4 hours to seconds.
  $dateFormat="d-m-Y H:i"; //set the date format
  $timeNdate=gmdate($dateFormat, time()-$offset); //get GMT date - 4
?>

I strongly UNIX farklı bir zaman dilimi gibi görünmesi için damgaları ile karıştırmasını kaçınarak öneririz. Bu ben zor yoldan, yol çok kez öğrendiğim bir derstir.

A damgası Midnight 1 Ocak 1970, GMT beri saniye sayısıdır. Eğer dünyanın neresinde önemli değil, belirli bir zaman damgası olursa olsun zaman dilimleri, zaman içinde aynı anı temsil eder. Evet, zaman damgası "0" Avustralya'da, Londra'da Midnight 1/1/70 olarak 10:00 1/1/70 ve LA 05:00 31/12/69 anlamına gelir, ama bu sadece değerleri ve garanti eklemek ya da çıkarmak anlamına gelmez doğruluk.

Şekilde her yıl için beni dolması altın örnek çok uzun ışığından çıkabilirler ne zaman oldu. Gün ışığından dünyanın belli bölgelerinde mevcut olmayan "saat zamanlar" var demektir. Örneğin, ABD'nin birçok yerinde, 02:01 gibi bir zaman 2 Nisan 2006 tarihinde oldu.

Yeter yarı anlaşılır ranting olsa. Benim tavsiyem o zaman damgaları gibi veritabanındaki tarihleri ​​depolamak ve için ...

Örneğin,

$timestamp = time();

echo "BRISBANE: " . date('r', $timestamp) . "\n";
echo "     UTC: " . gmdate('r', $timestamp) . "\n";
date_default_timezone_set('Africa/Johannesburg');
echo "  JOBURG: " . date('r', $timestamp) . "\n";

// BRISBANE: Tue, 12 May 2009 18:28:20 +1000
//      UTC: Tue, 12 May 2009 08:28:20 +0000
//   JOBURG: Tue, 12 May 2009 10:28:20 +0200

Bu (eğer uzaklıklar ekleyerek endişesi olmak zorunda değilsiniz, ve sonra kaydetmeden önce çıkarılarak) temiz değerlerini tutacak, tüm Günışığından kurallara saygı duyacağız, ve aynı zamanda da yerin zaman dilimini bakmak zorunda değilsiniz İstediğiniz.

Bu, size randevum ikinci argüman olarak damgası verecek bir veritabanında saklanan bir tarih dönüştürmek tarih ('Z') kullanıyorsanız, aksi halde sonuç doğru olmayacağını dikkat etmek önemlidir.

örneğin

İngiltere'de, bazen tarihi ('Z') bazen 0 verecektir, 3600 verecektir.

echo date('Z', strtotime('2012-01-01'));

0 verir

echo date('Z', strtotime('2012-07-01'));

3600 verir

Yani sadece ($ dbDateValue) strtotime kullanarak - date ('Z') yanlış olabilir

Bunun yerine kullanın:

$dbDateTimestamp = strtotime($dbDateValue); // where $dbDateValue something like 2012-01-01 22:34:00

$utcTimestamp = strtotime($dbDateTimestamp) - date('Z', $dbDateTimestamp);

Bu tabii ki PHP dayanır ve veritabanı aynı zaman dilimine göre ayarlı hem.

İşte GMT farklı olan zaman dilimleri bir liste, bu yararlı umut!

echo '<pre>';
$zones_array = array();        
$timestamp = time();         
# to maintain the original timezone, store before
$default_timezone = date_default_timezone_get();
foreach (timezone_identifiers_list() as $key => $zone) {    
    date_default_timezone_set($zone);
    $zones_array[$key]['zone'] = $zone;
    $zones_array[$key]['diff_from_GMT'] = date('P', $timestamp);
}
# to maintain the original timezone, re-set after
date_default_timezone_set($default_timezone);    
print_r($zones_array);

Teşekkürler!

Ben gerekli bir zaman dilimi için bir zaman damgası dönüştürmek için alternatif bir fonksiyon yazmak için bu soru esinlenmiştir.

Yani, DOĞRU prosedür adımlar (dönüştürmek), zaman görüntü kaydetmek için:

1) record timestamp with time()
2) If you need to get a different timezone, use:
   2.1) Class based:
       2.1.1) DateTime class + setTimezone
       or
       2.1.2) date_default_timezone_set() and then date()

       2.1.1) is better and more flexible than 2.1.2)

    Function:

      function getLTime_class($ts, $uTZ, $format_str="Y.m.d H:i", $sTZ="America/Toronto"){
        // $ts - timestamp recorded with time(); if have date => convert to timestamp: strtotime($date_input); //for ex strtotime('2012-02-17 12:00:00'); => 1345219200 - same value for summer and winter (for toronto timezone);
        // $uTZ - TZ string (ex: 'America/Toronto'); convert timestamp to this TimeZone
        // $sTZ - TZ string (ex: 'America/Toronto'); convert timestamp from this TimeZone
        $TimeZone_server=new DateTimeZone($sTZ);
        $date_obj=new DateTime("@$ts", $TimeZone_server);
        $TimeZone_local=new DateTimeZone($uTZ);
        $date_obj->setTimeZone($TimeZone_local);
        return $date_obj->format($format_str);
      }

    Usage:

     $date_input=$_POST['date_input']; //read from POST
     $ts=strtotime($date_input); //for ex strtotime('2012-02-17 12:00:00'); => 1345219200 - same value for summer and winter (for toronto timezone);
     $userTZ='America/Toronto'; //read from user params
     $result=getLTime_class($ts, $userTZ, 'Y-m-d H:i:s');


   2.2) Non-Class based:

Function:

      //this function replaces the function with DateTime class. It's faster. $uTZoffset is integer.
      function getLTime_nonclass($ts, $uTZoffset, $format_str="Y.m.d H:i"){
        // $ts - timestamp recorded with time(); if have date => convert to timestamp: strtotime($date_input); //for ex strtotime('2012-02-17 12:00:00'); => 1345219200 - same value for summer and winter (for toronto timezone);
        // $uTZoffset - TZ offset (integer) $uTZoffset must consider DTS (for ex: in summer $uTZoffset=-4; in winter $uTZoffset=-5)
        $ts_offset=date('Z',$ts);
        if ($uTZoffset) $add_offset=$ts_offset-date('Z'); //when not UTC
        else $add_offset=0; //when UTC
        return date($format_str,$ts-$ts_offset+$uTZoffset*3600+$add_offset);
      }

    Usage:

     $date_input=$_POST['date_input']; //read from POST
     $ts=strtotime($date_input); //for ex strtotime('2012-02-17 12:00:00'); => 1345219200 - same value for summer and winter (for toronto timezone);
     $uTZoffset=-4; //read from user params. $uTZoffset - TZ offset (integer) $uTZoffset must consider DTS (for ex: in summer $uTZoffset=-4; in winter $uTZoffset=-5)
     $result=getLTime_nonclass($ts, $uTZoffset, 'Y-m-d H:i:s');

Sonuçlar:

$ Date_input = 2012-08-17 12:00:00

Server date: summer (2013-08-26)
    getLTime_class      =>  $userTZ='America/Toronto'   =>  2012-08-17 12:00:00
    getLTime_nonclass   =>  $uTZoffset=-4               =>  2012-08-17 12:00:00
    getLTime_class      =>  $userTZ='UTC'               =>  2012-08-17 16:00:00
    getLTime_nonclass   =>  $uTZoffset=0                =>  2012-08-17 16:00:00

Server date: winter (2013-02-26)
    getLTime_class      =>  $userTZ='America/Toronto'   =>  2012-08-17 12:00:00
    getLTime_nonclass   =>  $uTZoffset=-5               =>  2012-08-17 12:00:00
    getLTime_class      =>  $userTZ='UTC'               =>  2012-08-17 16:00:00
    getLTime_nonclass   =>  $uTZoffset=0                =>  2012-08-17 16:00:00

$ Date_input = 2012-02-17 12:00:00

Server date: summer (2013-08-26)
    getLTime_class      =>  $userTZ='America/Toronto'   =>  2012-02-17 12:00:00
    getLTime_nonclass   =>  $uTZoffset=-4               =>  2012-02-17 12:00:00
    getLTime_class      =>  $userTZ='UTC'               =>  2012-02-17 17:00:00
    getLTime_nonclass   =>  $uTZoffset=0                =>  2012-02-17 17:00:00

Server date: winter (2013-02-26)    
    getLTime_class      =>  $userTZ='America/Toronto'   =>  2012-02-17 12:00:00
    getLTime_nonclass   =>  $uTZoffset=-5               =>  2012-02-17 12:00:00
    getLTime_class      =>  $userTZ='UTC'               =>  2012-02-17 17:00:00
    getLTime_nonclass   =>  $uTZoffset=0                =>  2012-02-17 17:00:00 

I decided to getLTime_nonclass hoping to make the conversion faster than using classes. All I ask is to confirm that getLTime_nonclass is a valid replacement for getLTime_class. Please feel free to express your opinion. Thanks