Kelas DateTimeZone
Kelas DateTimeZone membolehkan anda bekerja dengan zon waktu dalam PHP.
Menerima rentetan pengenal zon waktu dalam pembinanya.
Sintaks
new DateTimeZone(string $timezone);
Contoh
Mari buat objek zon waktu untuk Moscow:
<?php
$timezone = new DateTimeZone('Europe/Moscow');
print_r($timezone);
?>
Keputusan pelaksanaan kod:
DateTimeZone Object
(
'timezone_type' => 3
'timezone' => Europe/Moscow
)
Contoh
Penggunaan dengan kelas DateTime:
<?php
$timezone = new DateTimeZone('America/New_York');
$date = new DateTime('now', $timezone);
echo $date->format('Y-m-d H:i:s');
?>
Keputusan pelaksanaan kod (waktu semasa di New York):
'2025-06-15 14:30:00'
Contoh
Mendapatkan senarai semua zon waktu yang tersedia:
<?php
$timezones = DateTimeZone::listIdentifiers();
print_r(array_slice($timezones, 0, 5));
?>
Keputusan pelaksanaan kod:
[
'Africa/Abidjan'
'Africa/Accra'
'Africa/Addis_Ababa'
'Africa/Algiers'
'Africa/Asmara'
]
Contoh
Mendapatkan ofset zon waktu dari UTC:
<?php
$timezone = new DateTimeZone('Asia/Tokyo');
echo $timezone->getOffset($date) / 3600;
?>
Keputusan pelaksanaan kod:
9
Lihat juga
-
kelas
DateTime,
untuk bekerja dengan tarikh dan waktu -
kelas
DateInterval,
yang menentukan selang waktu -
fungsi
date_default_timezone_set,
untuk menetapkan zon waktu lalai