Phương thức setTimezone của lớp DateTime
Phương thức setTimezone thay đổi múi giờ của đối tượng DateTime. Nó nhận một đối tượng của lớp DateTimeZone làm tham số. Trả về đối tượng DateTime đã được sửa đổi.
Cú pháp
public DateTime::setTimezone(DateTimeZone $timezone): DateTime
Ví dụ
Hãy thiết lập múi giờ 'America/New_York' cho ngày hiện tại:
<?php
$date = new DateTime('now', new DateTimeZone('UTC'));
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format('Y-m-d H:i:s');
?>
Kết quả thực thi mã:
'2023-11-15 10:30:00'
Ví dụ
Hãy thay đổi múi giờ của một đối tượng DateTime có sẵn:
<?php
$date = new DateTime('2023-01-01 12:00:00', new DateTimeZone('Europe/Moscow'));
$date->setTimezone(new DateTimeZone('Asia/Tokyo'));
echo $date->format('Y-m-d H:i:s');
?>
Kết quả thực thi mã:
'2023-01-01 18:00:00'
Xem thêm
-
hàm
date,
hàm định dạng ngày/giờ -
hàm
date_default_timezone_set,
hàm thiết lập múi giờ mặc định -
lớp
DateTimeZone,
lớp đại diện cho múi giờ