Hướng dẫn php new datetime utc - php datetime mới utc

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

// 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');
2 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_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');
3 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ư
// 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');
2.

// 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_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');
5

$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_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');
6 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_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');
7 Trả về ngày trong múi giờ địa phương hiện tại.

// 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');
8 Trả về ngày ở UTC

// 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');
9 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

https://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

http://php.net/manual/en/function.date.php

Cập nhật php timezone db (trong Linux)

sudo pecl install timezonedb

Vì 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.

Cách dễ nhất để có được phần bù UTC trong PHP, so với múi giờ (hệ thống) hiện tại là gì?

Nội dung chính

  • Làm thế nào để có được thời gian offset php?
  • Làm thế nào để tôi tìm thấy bù UTC của tôi?
  • Làm thế nào tôi có thể nhận được UTC trong PHP?
  • Làm thế nào để tôi nhận được thời gian bù thời gian?

Phương thức JavaScript getTimeZoneOffset () được sử dụng để tìm phần bù múi giờ.Nó trả về sự khác biệt về múi giờ trong vài phút, giữa UTC và giờ địa phương hiện tại.Nếu giá trị trả về là dương, timezone cục bộ đứng sau UTC và nếu nó âm, thì timezone địa phương nếu đi trước UTC. is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, local timezone is behind the UTC and if it is negative, the local timezone if ahead of UTC.Oct 11, 2008 at 0:58

  date('Z');

Cách dễ nhất để có được phần bù UTC trong PHP, so với múi giờ (hệ thống) hiện tại là gì?

Nội dung chínhOct 11, 2008 at 1:06

Làm thế nào để có được thời gian offset php?Czimi

Hỏi ngày 11 tháng 10 năm 2008 lúc 0:58Oct 11, 2008 at 0:5816 silver badges14 bronze badges

4

// will output something like +02:00 or -04:00
echo date('P');

Trả về độ lệch UTC trong giây.

Đã trả lời ngày 11 tháng 10 năm 2008 lúc 1:06Oct 11, 2008 at 1:0615 gold badges123 silver badges147 bronze badges

CzimiczimiCzimiMar 15, 2016 at 20:17

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');
0

$this_tz_str = date_default_timezone_get();
$this_tz = new DateTimeZone($this_tz_str);
$now = new DateTime("now", $this_tz);
$offset = $this_tz->getOffset($now);

2.39416 Huy hiệu bạc14 Huy hiệu đồng16 silver badges14 bronze badges

Nội dung chínhOct 11, 2008 at 1:06

Làm thế nào để có được thời gian offset php?John Millikin

Hỏi ngày 11 tháng 10 năm 2008 lúc 0:58Oct 11, 2008 at 0:5839 gold badges211 silver badges222 bronze badges

1

Trả về độ lệch UTC trong giây.

date_default_timezone_set('America/New_York');
$utc_offset =  date('Z') / 3600;

Đã trả lời ngày 11 tháng 10 năm 2008 lúc 1:06Oct 11, 2008 at 1:06

CzimiczimiCzimi

2.39416 Huy hiệu bạc14 Huy hiệu đồng16 silver badges14 bronze badgesJun 9, 2015 at 15:08

Ole V.V.Kenny

77.9K15 Huy hiệu vàng123 Huy hiệu bạc147 Huy hiệu đồng15 gold badges123 silver badges147 bronze badges2 gold badges21 silver badges30 bronze badges

Đã trả lời ngày 15 tháng 3 năm 2016 lúc 20:17Mar 15, 2016 at 20:17

// 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

Chưa được kiểm tra, nhưng nên hoạt độngMar 23, 2018 at 17:31

CzimiczimiJohn MillikinHMagdy

2.39416 Huy hiệu bạc14 Huy hiệu đồng39 gold badges211 silver badges222 bronze badges33 silver badges54 bronze badges

2

77.9K15 Huy hiệu vàng123 Huy hiệu bạc147 Huy hiệu đồ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');
1

Đã trả lời ngày 15 tháng 3 năm 2016 lúc 20:17May 29, 2017 at 21:33

Chưa được kiểm tra, nhưng nên hoạt độngJun 9, 2015 at 15:08زياد

John Millikinjohn MillikinKenny10 silver badges14 bronze badges

19

Phim thương hiệu vàng 193K392 gold badges21 silver badges30 bronze badges

// 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

Tôi đã làm một phiên bản sửa đổi một chút về những gì Oscar đã làm.

// 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

Điều này đã cho tôi sự bù đắp từ múi giờ của tôi, EST, đến UTC, trong vài giờ.Mar 23, 2018 at 17:31

// will output something like +02:00 or -04:00
echo date('P');
0:

Giá trị của $ UTC_OFFSET là -4.HMagdy

  • Đã trả lời ngày 9 tháng 6 năm 2015 lúc 15:0833 silver badges54 bronze badges
  • Kennykenny
  • 2.1202 Huy hiệu vàng21 Huy hiệu bạc 30 Huy hiệu ĐồngMay 29, 2017 at 21:33

Đơn giản là bạn có thể làm điều này:زيادApr 7 at 5:23

Đã trả lời ngày 23 tháng 3 năm 2018 lúc 17:3110 silver badges14 bronze badges

HmagdyhmagdyJesse יִשַׁי

2.92133 huy hiệu bạc54 Huy hiệu đồng

Đây là cùng chức năng JavaScript 1 gold badge7 silver badges22 bronze badges

Đã trả lời ngày 29 tháng 5 năm 2017 lúc 21:33

// 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');
1

زازاد

89210 Huy hiệu bạc14 Huy hiệu đồngJan 5, 2013 at 15:55

Điều này sẽ xuất ra một cái gì đó được định dạng là:

// will output something like +02:00 or -04:00
echo date('P');
1 hoặc
// will output something like +02:00 or -04:00
echo date('P');
2:Amr

Điều này có thể hữu ích cho định dạng RSS RFC822 thích hợpApr 7 at 5:236 gold badges44 silver badges58 bronze badges

3

Làm thế nào để có được thời gian offset php?

Hỏi ngày 11 tháng 10 năm 2008 lúc 0:58Oct 11, 2008 at 0:58timezone_offset_get() function is an inbuilt function in PHP which is used to return the timezone offset from GMT. The date time object and the date-time are sent as a parameter to the timezone_offset_get() function and return the timezone offset in seconds on success or False on failure.

Làm thế nào để tôi tìm thấy bù UTC của tôi?

Sự khác biệt này được thể hiện liên quan đến UTC và thường được thể hiện ở định dạng ± [hh]: [mm], ± [hh] [mm] hoặc ± [hh].Vì vậy, nếu thời gian được mô tả là hai giờ trước UTC (chẳng hạn như ở Kigali, Rwanda [xấp xỉ 30 ° E]), thì UTC sẽ là "+02: 00", "+0200", hoặc đơn giản là "+02".±[hh]:[mm], ±[hh][mm], or ±[hh]. So if the time being described is two hours ahead of UTC (such as in Kigali, Rwanda [approx. 30° E]), the UTC offset would be "+02:00", "+0200", or simply "+02".±[hh]:[mm], ±[hh][mm], or ±[hh]. So if the time being described is two hours ahead of UTC (such as in Kigali, Rwanda [approx. 30° E]), the UTC offset would be "+02:00", "+0200", or simply "+02".

Làm thế nào tôi có thể nhận được UTC trong PHP?

Sử dụng DATE_DEFAULT_TIMEZONE_SET () để có thời gian UTC, hàm date_default_timezone_set () được sử dụng để thay đổi múi giờ.Chúng tôi cũng có thể sử dụng nó để trả lại UTC bằng cách thay đổi tham số của nó thành UTC.Kết quả là, nó sẽ luôn luôn ở UTC, không phân biệt thời gian trong tập lệnh PHP. The date_default_timezone_set() function is used to change the time zone. We can also use it to return the UTC by changing its parameter to UTC . As a result, it'll always be in UTC, irrespective of the time in the PHP script. The date_default_timezone_set() function is used to change the time zone. We can also use it to return the UTC by changing its parameter to UTC . As a result, it'll always be in UTC, irrespective of the time in the PHP script.

Làm thế nào để tôi nhận được thời gian bù thời gian?

Phương thức JavaScript getTimeZoneOffset () được sử dụng để tìm phần bù múi giờ.Nó trả về sự khác biệt về múi giờ trong vài phút, giữa UTC và giờ địa phương hiện tại.Nếu giá trị trả về là dương, timezone cục bộ đứng sau UTC và nếu nó âm, thì timezone địa phương nếu đi trước UTC. is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, local timezone is behind the UTC and if it is negative, the local timezone if ahead of UTC. is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, local timezone is behind the UTC and if it is negative, the local timezone if ahead of UTC.