Hướng dẫn dùng timezone slovakia trong PHP

simple question, but cannot seem to find an answer

I need to set a timezone to UTC-8 and only UTC-8 (GMT-8 is actually the same). It cannot be a timezone with daylight time saving.

Now PHP does not seem to provide a way to set to GMT-8, when using Etc/GMT-8 I get the wrong time (I get an offset way earlier) like it says in the DOCS

Now I could use GMT+8 to achieve GMT-8 (sounds odd but i Quote:

Warning If you disregard the above warning, please also note that the IANA timezone database that provides PHP's timezone support uses POSIX style signs, which results in the Etc/GMT+n and Etc/GMT-n time zones being reversed from common usage. For example, the time zone 8 hours ahead of GMT that is used in China and Western Australia (among other places) is actually Etc/GMT-8 in this database, not Etc/GMT+8 as you would normally expect.

as there is no country in exactly UTC-8, i cannot put a country there...

so I use Etc/GMT+8 and cross the fingers they wont change it?

asked Mar 10, 2017 at 18:23

Hướng dẫn dùng timezone slovakia trong PHP

1

Yes, you can absolutely use Etc/GMT+8 for a time zone that is fixed to UTC-8. No, it won't change. IANA time zone names are identifiers, and they are guaranteed to not be removed from the tz database.

That said, I'd ask you to question why you need a UTC-8 fixed offset time zone:

  • Is it because you are referencing time on Pitcairn Islands (population 49)? If so, use Pacific/Pitcairn.

  • Is it because you are calculating time for a ship at sea that is traveling between 112.5°W and 127.5°W latitude? If so, use Etc/GMT+8.

  • Is it because you think DST isn't important, or you don't like it, or you were given a requirement that doesn't include it, etc? Sorry, but like it or not - the people who physically occupy these areas do use DST. If you elect not to, your clock will disagree with theirs for a considerable part of the year. Use America/Los_Angeles, America/Tijuana, America/Vancouver, America/Dawson, or America/Whitehorse.

answered Mar 12, 2017 at 6:12

Matt Johnson-PintMatt Johnson-Pint

218k73 gold badges428 silver badges544 bronze badges

1

Not the answer you're looking for? Browse other questions tagged php date datetime timezone or ask your own question.

date_timezone_set

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

Nội dung chính

  • date_timezone_set
  • Return Values
  • TIMEZONE LÀ GÌ?
  • CÁCH CHỈNH TIMEZONE TRÊN LINUX
  • Xác định giờ đang chạy trên Linux
  • Xác định thư mục chứa timezone
  • Đồng bộ giờ qua internet với các NTP server
  • CÁCH CHỈNH TIMEZONE CHO PHP TẠI PHP.INI
  • CÁCH ĐỔI TIMEZONE TRONG CPANEL HOSTING 123HOST

DateTime::setTimezone -- date_timezone_setSets the time zone for the DateTime object

Parameters

object

Procedural style only: A DateTime object returned by date_create(). The function modifies this object.

timezone

A DateTimeZone object representing the desired time zone.

Return Values

Returns the DateTime object for method chaining. The underlaying point-in-time is not changed when calling this method.

Examples

Example #1 DateTime::setTimeZone() example

Object-oriented style

$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo 
$date->format('Y-m-d H:i:sP') . "\n";$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo 
$date->format('Y-m-d H:i:sP') . "\n";
?>

Procedural style

$date date_create('2000-01-01'timezone_open('Pacific/Nauru'));
echo 
date_format($date'Y-m-d H:i:sP') . "\n";date_timezone_set($datetimezone_open('Pacific/Chatham'));
echo 
date_format($date'Y-m-d H:i:sP') . "\n";
?>

The above examples will output:

2000-01-01 00:00:00+12:00
2000-01-01 01:45:00+13:45

See Also

  • DateTimeImmutable::setTimezone() - Sets the time zone
  • DateTime::getTimezone() - Return time zone relative to given DateTime
  • DateTimeZone::__construct() - Creates new DateTimeZone object

There are no user contributed notes for this page.

Đôi lúc các bạn xem các hướng dẫn về hàm lấy thời gian :date(format,timestamp).

Nếu chưa biết hàm date(format,timestamp) bạn có thể xem ở link:http://congnghe5s.com/bai-viet/xu-ly-ngay-gio-trong-php.html.

Bạn thực hiện test lệnh trên máy và kết quả không khớp với time hiện tại. Đó là vì lí do thời gian hay timezone trên server bạn đã cài đặt khác vì thế để trả về đúng time bạn muốn bạn có thể dùng hàm:

Cú pháp:date_default_timezone_set ( string $timezone_identifier ).

tham số : $timezone_identifier bạn lấy ở đây:http://php.net/manual/en/timezones.asia.php

phù hợp khu vực bạn muốn ví dụ mình muốn set lấy giờ việt nam mình làm như sau:

date_default_timezone_set('Asia/Ho_Chi_Minh').

Sau đó các bạn dùng hàm : echo date('d/m/Y - H:i:s');

ra đúng kết quả time ở Việt Nam.

Ví dụ:

date_default_timezone_set('Asia/Ho_Chi_Minh');

if (date_default_timezone_get()) {
    echo 'date_default_timezone_set: ' . date_default_timezone_get() . '
';
}
echo date('d/m/Y H:i:s');

Ví dụ trên có 1 hàm cũng khá quan trọng : date_default_timezone_get()  trả về timezone mặc định của server bạn đang chạy hoặc là đã được thiết lập thông qua date_default_timezone_set().

Qua bài viết hy vọng các bạn có thêm kiến thức khi xử lý ngày tháng trong PHP.

Chào các bạn,

Hôm nay Kỹ Thuật xin giới thiệu đến các bạn về timezone của PHP và cách chỉnh timezone trên VPS lẫn Hosting.

Contents

  • 1 TIMEZONE LÀ GÌ?
  • 2 CÁCH CHỈNH TIMEZONE TRÊN LINUX
    • 2.1 Xác định giờ đang chạy trên Linux
    • 2.2 Xác định thư mục chứa timezone
    • 2.3 Đồng bộ giờ qua internet với các NTP server
  • 3 CÁCH CHỈNH TIMEZONE CHO PHP TẠI PHP.INI
  • 4 CÁCH ĐỔI TIMEZONE TRONG CPANEL HOSTING 123HOST
    • 4.1 Related

TIMEZONE LÀ GÌ?

  • Một múi giờ là một vùng trên Trái Đất mà người ta quy ước sử dụng cùng một thời gian tiêu chuẩn, thông thường được nói đến như là giờ địa phương. Về lý thuyết, các đồng hồ tại vùng này luôn chỉ cùng một thời gian.
  • Hiện nay trên thế giới có vài loại quy ước về timezone:
    • GMT: được quy ước lần đầu tiên tại Anh vào thế kỷ 19.
    • UTC: được thực hiện bằng phương pháp nguyên tử. Là hệ thống thời gian dùng trong nhiều chuẩn internet và WWW. Đặc biệt là giao thức giờ trên mạng – NTP được thiết kế để phân phối tự động giờ trên internet.

CÁCH CHỈNH TIMEZONE TRÊN LINUX

Xác định giờ đang chạy trên Linux

Để xác định giờ trên Linux, bạn hãy dùng lệnh date.

Xác định thư mục chứa timezone

Trên Linux có một thư mục chứa thông tin cài đặt timezone nằm tại vị trí /usr/share/zoneinfo/

Xác định được múi giờ mình muốn đổi và copy file timezone đó đè về file /etc/localtime:

cp /usr/share/zoneinfo/Asia/Ho_Chi_Minh /etc/localtime

Kiểm tra lại với lệnh date để xem ngày giờ đã hiển thị đúng chưa.

Đồng bộ giờ qua internet với các NTP server

Tương tự như Window, Linux cũng cho phép đồng bộ giờ với các NTP server trên thế giới.

Ta cần dùng phương pháp này khi mà đã chọn đúng timezone nhưng trước đó ta có dùng lệnh date để chỉnh giờ và hiển thị không đúng kết quả mong muốn.

Trước tiên cần cài gói ntp, dùng lệnh yum:

yum install -y ntp

Khởi động dịch vụ:

service ntpd start

Chạy lệnh tiếp theo để đồng bộ giờ qua internet:

ntpdate vn.pool.ntp.org

(Trong đó vn.pool.ntp.org là server để đồng bộ, muốn đồng bộ giờ đúng thì trước tiên phải chọn timezone đúng copy vào /etc/localtime)

CÁCH CHỈNH TIMEZONE CHO PHP TẠI PHP.INI

Trước tiên cần tìm file php.ini trên máy chủ nằm ở đâu, gõ lệnh:

php -i | grep ini

Mở file php.ini theo đường dẫn.

vi /usr/local/etc/php5/cgi/php.ini

Thêm dòng lệnh vào đầu file và lưu lại:

date_default_timezone_set('Asia/Ho_Chi_Minh');

(Với Asia/Ho_Chi_Minh là timezone muốn cài đặt, có thể tham khảo thêm các timezone khác tại đây: http://php.net/manual/en/timezones.php)
Nếu không thay đổi trong file php.ini, bạn có thể đặt lệnh đó vào trong code php của bạn để tùy biến timezone theo từng mã nguồn.

CÁCH ĐỔI TIMEZONE TRONG CPANEL HOSTING 123HOST

Đăng nhập vào cpanel hosting 123host.

Tìm đến phần Software > Select php version

Chọn tiếp Switch To PHP Options

Tại dòng date.timezone, ta nhập vào timezone muốn thay đổi (tên các timezone tương tự trong file php.ini) và nhấn Apply, sau đó nhấn Save để lưu lại.

Để kiểm tra giờ đã đổi đúng chưa khi đổi trong file php.ini và trên cpanel hosting, ta sẽ tạo một file gettime.php đơn giản với nội dung:

echo "The time is " . date('l dS \of F Y h:i:s A');

?>

Kỹ thuật đã hướng dẫn xong cách chỉnh timezone trên các môi trường khác nhau.

Chúc các bạn thành công!

Related