In my case, I'm not sure I can guess the correct timezone any better than PHP and it's no where near important enough to nag the user, so...
<?php
// Suppress DateTime warnings
date_default_timezone_set(@date_default_timezone_get());
?>
date_default_timezone_get
(PHP 5 >= 5.1.0)
date_default_timezone_get — Betik içerisindeki tüm tarih/zaman işlevleri tarafından kullanılan öntanımlı zaman dilimini getirir
Açıklama
Bu işlev tercih sırasına göre öntanımlı zaman dilimini şöyle döndürür:
-
date_default_timezone_set() işlevi kullanılarak atanan zaman dilimiyle (eğer varsa).
-
TZ ortam değişkenindeki değerle (boş değilse).
-
date.timezone ini yönergesindeki değerle (ayarlanmışsa).
-
Ev sahibi işletim sistemini sorgulayarak (işletim sistemi destekliyor ve izin veriyorsa).
Eğer yukarıdakilerin hiçbirisi başarılı değilse, date_default_timezone_get() işlevi öntanımlı UTC zaman dilimini döndürecektir.
Dönen Değerler
string türünde geri döner.
Örnekler
Örnek 1 - Öntanımlı zaman diliminin getirilmesi
<?php
date_default_timezone_set('Europe/Istanbul');
if (date_default_timezone_get()) {
echo 'date_default_timezone_set: '. date_default_timezone_get() .'<br />';
}
if (ini_get('date.timezone')) {
echo 'date.timezone: ' . ini_get('date.timezone');
}
?>
Yukarıdaki örnek şuna benzer bir çıktı üretir:
date_default_timezone_set: Europe/Istanbul date.timezone: Europe/Istanbul
Örnek 2 - Zaman diliminin kısaltmasının getirilmesi
<?php
date_default_timezone_set('Europe/Istanbul');
echo date_default_timezone_get() . ' => ' . date('e') . ' => ' . date('T');
?>
Yukarıdaki örneğin çıktısı:
Europe/Istanbul => Europe/Istanbul => EET
Ayrıca Bakınız
- date_default_timezone_set() - Betik içerisindeki tüm tarih/zaman işlevleri tarafından kullanılan öntanımlı zaman dilimini ayarlar
- Desteklenen Zaman Dilimlerinin Listesi
To get offset string from offset:
<?php
function timezone_offset_string( $offset )
{
return sprintf( "%s%02d:%02d", ( $offset >= 0 ) ? '+' : '-', abs( $offset / 3600 ), abs( $offset % 3600 ) );
}
$offset = timezone_offset_get( new DateTimeZone( 'Pacific/Kiritimati' ), new DateTime() );
echo "offset: " . timezone_offset_string( $offset ) . "\n";
$offset = timezone_offset_get( new DateTimeZone( 'Pacific/Tahiti' ), new DateTime() );
echo "offset: " . timezone_offset_string( $offset ) . "\n";
?>
Output:
offset: +14:00
offset: -10:00
If you want to get the abbrivation (3 or 4 letter), instead of the long timezone string you can use date('T') function like this:
Input:
date_default_timezone_set('America/Los_Angeles');
echo date_default_timezone_get();
echo ' => '.date('e');
echo ' => '.date('T');
Output:
America/Los_Angeles => America/Los_Angeles => PST
date_default_timezone_get() will still emit a warning in E_STRICT if the timezone is not set; either by date_default_timezone_set() or the ini option of date.timezone.
This is probably not a big deal, but I thought I would contribute what I found.
