Nếu theo khách hàng, bạn có nghĩa là trình duyệt, thì trước tiên bạn cần gửi tên múi giờ đến PHP từ trình duyệt, sau đó thực hiện chuyển đổi như được mô tả dưới đây.
Câu trả lời
Chuyển đổi DateTime UTC sang Mỹ/Denver
// create a $dt object with the UTC timezone $dt = new DateTime('2016-12-12 12:12:12', new DateTimeZone('UTC')); // change the timezone of the object without changing its time $dt->setTimezone(new DateTimeZone('America/Denver')); // format the datetime $dt->format('Y-m-d H:i:s T');Điều này hoạt động với ngày sau năm 2032, tiết kiệm ánh sáng ban ngày và những giây nhảy vọt và không phụ thuộc vào địa phương máy chủ hoặc múi giờ.
Nó sử dụng thời gian để thực hiện tính toán, DB này thay đổi theo thời gian khi các quy tắc thời gian thay đổi và phải được cập nhật. (Xem ghi chú ở phía dưới)
Để chuyển đổi ngày UTC sang thời gian máy chủ (cục bộ), bạn có thể sử dụng DateTime mà không cần đối số thứ hai, mặc định là thời gian của máy chủ.
// create a $dt object with the UTC timezone $dt = new DateTime('2016-12-12 12:12:12', new DateTimeZone('UTC')); // get the local timezone $loc = (new DateTime)->getTimezone(); // change the timezone of the object without changing its time $dt->setTimezone($loc); // format the datetime $dt->format('Y-m-d H:i:s T');Trả lời 2
Tôi khuyên bạn nên sử dụng // create a $dt object with the UTC timezone $dt = new DateTime('2016-12-12 12:12:12', new DateTimeZone('UTC')); // get the local timezone $loc = (new DateTime)->getTimezone(); // change the timezone of the object without changing its time $dt->setTimezone($loc); // format the datetime $dt->format('Y-m-d H:i:s T'); 0 vì nó không thay đổi các biến (không thay đổi chúng đằng sau hậu trường), nếu không nó hoạt động giống như DateTime.
// create a $dt object with the UTC timezone $dt_utc = new DateTimeImmutable('2016-12-12 12:12:12', new DateTimeZone('UTC')); // Create a new instance with the new timezone $dt_denver = $dt_utc->setTimezone(new DateTimeZone('America/Denver')); // format the datetime $dt_denver->format('Y-m-d H:i:s T');Khả năng bất biến cho phép bạn sử dụng chuỗi nhiều lần mà không thay đổi giá trị của // create a $dt object with the UTC timezone $dt = new DateTime('2016-12-12 12:12:12', new DateTimeZone('UTC')); // get the local timezone $loc = (new DateTime)->getTimezone(); // change the timezone of the object without changing its time $dt->setTimezone($loc); // format the datetime $dt->format('Y-m-d H:i:s T'); 2
$dt = new DateTimeImmutable('2016-12-12 12:12:12', new DateTimeZone('UTC')); // Format $dt in Denver timezone echo $dt->setTimezone(new DateTimeZone('America/Denver'))->format('Y-m-d H:i:s T'); // Format $dt in Madrid timezone echo $dt->setTimezone(new DateTimeZone('Europe/Madrid'))->format('Y-m-d H:i:s T'); // Format $dt in Local server timezone echo $dt->setTimezone((new DateTime())->getTimezone())->format('Y-m-d H:i:s T');Ghi chú
// create a $dt object with the UTC timezone $dt = new DateTime('2016-12-12 12:12:12', new DateTimeZone('UTC')); // get the local timezone $loc = (new DateTime)->getTimezone(); // change the timezone of the object without changing its time $dt->setTimezone($loc); // format the datetime $dt->format('Y-m-d H:i:s T'); 3 trả về dấu thời gian UNIX, đó là một con số, nó không có múi giờ.
// create a $dt object with the UTC timezone $dt = new DateTime('2016-12-12 12:12:12', new DateTimeZone('UTC')); // get the local timezone $loc = (new DateTime)->getTimezone(); // change the timezone of the object without changing its time $dt->setTimezone($loc); // format the datetime $dt->format('Y-m-d H:i:s T'); 4 Trả về ngày trong múi giờ địa phương hiện tại.
// create a $dt object with the UTC timezone $dt = new DateTime('2016-12-12 12:12:12', new DateTimeZone('UTC')); // get the local timezone $loc = (new DateTime)->getTimezone(); // change the timezone of the object without changing its time $dt->setTimezone($loc); // format the datetime $dt->format('Y-m-d H:i:s T'); 5 Trả về ngày ở UTC
// create a $dt object with the UTC timezone $dt = new DateTime('2016-12-12 12:12:12', new DateTimeZone('UTC')); // get the local timezone $loc = (new DateTime)->getTimezone(); // change the timezone of the object without changing its time $dt->setTimezone($loc); // format the datetime $dt->format('Y-m-d H:i:s T'); 6 thay đổi múi giờ địa phương hiện tại
Để thay đổi thời gian trong một múi giờ
// create a $dt object with the America/Denver timezone $dt = new DateTime('2016-12-12 12:12:12', new DateTimeZone('America/Denver')); // change the timezone of the object without changing it's time $dt->setTimezone(new DateTimeZone('UTC')); // format the datetime $dt->format('Y-m-d H:i:s T');Ở đây bạn có thể thấy tất cả các múi giờ có sẵn
//en.wikipedia.org/wiki/List_of_tz_database_time_zones
Dưới đây là tất cả các tùy chọn định dạng
//php.net/manual/en/function.date.php
Cập nhật php timezone db (trong Linux)
sudo pecl install timezonedbVì tiết kiệm ánh sáng ban ngày, một số ngày lặp lại trong một số múi giờ, ví dụ, tại Hoa Kỳ, ngày 13 tháng 3 năm 2011 2:15 AM chưa bao giờ xảy ra, trong khi ngày 6 tháng 11 năm 2011 1:15 sáng xảy ra hai lần. Những dữ liệu này không thể được xác định chính xác.
Bạn có thể sử dụng Convert_tz trong các truy vấn MySQL, tuy nhiên nó yêu cầu lần đầu tiên điền vào các bảng múi giờ vào MySQL (bằng cách đọc chúng từ HĐH). Bạn cũng cần chạy lại điều này theo định kỳ vì một số thay đổi theo thời gian. Xem công cụ lệnh mysql_tzinfo_to_sql của MySQL.
Tôi không thích phương pháp này, mặc dù ID sử dụng nó nếu tôi phải chuyển đổi nhiều dữ liệu theo đợt
Nếu bạn chỉ muốn làm điều đó theo PHP, cách dễ nhất là sử dụng lớp DateTime ..
$tz_from = new DateTimeZone('UTC'); $tz_to = new DateTimeZone('Europe/London'); $orig_time = new DateTime($mysql_timestamp, $tz_from); $new_time = $orig_time->setTimezone($tz_to); $newtimestamp = $new_time->format('c');Chuyển đổi múi giờ sang một múi giờ cụ thể giúp đồng bộ hóa các loại khác nhau. Nó rất hữu ích để cân bằng thời gian máy chủ và thời gian địa phương của người dùng. Lớp DateTime PHP cung cấp một cách dễ dàng để chuyển đổi dấu thời gian ngày thành UTC. Bạn có thể chuyển đổi bất kỳ timezone nào sang DateTime UTC bằng PHP.PHP DateTime class provides an easy way to convert a date time stamp to UTC. You can convert any timezone to UTC DateTime using PHP.
Trong đoạn mã ví dụ, chúng tôi sẽ chỉ cho bạn cách chuyển đổi ngày & giờ địa phương sang DateTime UTC (định dạng Yyyy-MM-DD HH: MM: SS) trong PHP.
Chuyển đổi DateTime hiện tại thành UTC DateTime bằng PHP:
$dateTime = date("Y-m-d H:i:s");$newDateTime = new DateTime($dateTime);
$newDateTime->setTimezone(new DateTimeZone("UTC"));
$dateTimeUTC = $newDateTime->format("Y-m-d H:i:s");
Chuyển đổi DateTime cục bộ sang UTC DateTime bằng PHP:
$dateTime = '2021-04-28 18:37:54';$tz_from = 'America/New_York';
$newDateTime = new DateTime($dateTime, new DateTimeZone($tz_from));
$newDateTime->setTimezone(new DateTimeZone("UTC"));
$dateTimeUTC = $newDateTime->format("Y-m-d H:i:s");