Hướng dẫn get default timezone php - lấy múi giờ mặc định php

(Php 5> = 5.1.0, Php 7, Php 8)

date_default_timezone_get - Nhận thời gian mặc định được sử dụng bởi tất cả các hàm ngày/giờ trong tập lệnh Gets the default timezone used by all date/time functions in a script

Sự mô tả

date_default_timezone_get (): Chuỗi(): string

  • Đọc bộ timezone bằng hàm date_default_timezone_set () (nếu có)date_default_timezone_set() function (if any)

  • Đọc giá trị của ngày.Timezone Ini (nếu đặt)

Nếu không có điều nào ở trên thành công, DATE_DEFAULT_TIMEZONE_GET () sẽ trả về thời gian mặc định là UTC.date_default_timezone_get() will return a default timezone of UTC.

Thông số

Chức năng này không có tham số.

Trả về giá trị

Trả về một chuỗi.string.

Ví dụ

Ví dụ #1 Nhận thời gian mặc định

date_default_timezone_set('Europe/London');

America/Los_Angeles => America/Los_Angeles => PST
0

America/Los_Angeles => America/Los_Angeles => PST
1

Ví dụ trên sẽ xuất ra một cái gì đó tương tự như:

date_default_timezone_set: Europe/London
date.timezone: Europe/London

Ví dụ #2 Nhận chữ viết tắt của múi giờ

America/Los_Angeles => America/Los_Angeles => PST
2

Ví dụ trên sẽ xuất ra:

America/Los_Angeles => America/Los_Angeles => PST

Xem thêm

  • date_default_timezone_set () - Đặt múi giờ mặc định được sử dụng bởi tất cả các hàm ngày/giờ trong tập lệnh
  • Danh sách thời gian được hỗ trợ

Không có người dùng đóng góp ghi chú cho trang này.

Đô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).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 

America/Los_Angeles => America/Los_Angeles => PST
3 ).ú pháp:date_default_timezone_set ( string 
America/Los_Angeles => America/Los_Angeles => PST
3
 ).

tham số : $

America/Los_Angeles => America/Los_Angeles => PST
4http://php.net/manual/en/timezones.asia.php
America/Los_Angeles => America/Los_Angeles => PST
4http://php.net/manual/en/timezones.asia.php

America/Los_Angeles => America/Los_Angeles => PST
5

date_default_timezone_set('Asia/Ho_Chi_Minh').Asia/Ho_Chi_Minh').

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

ra đúng kết quả time ở Việt Nam.đú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().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.

Đô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).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 

America/Los_Angeles => America/Los_Angeles => PST
3 ).ú pháp:date_default_timezone_set ( string 
America/Los_Angeles => America/Los_Angeles => PST
3
 ).

tham số : $

America/Los_Angeles => America/Los_Angeles => PST
4http://php.net/manual/en/timezones.asia.php
America/Los_Angeles => America/Los_Angeles => PST
4http://php.net/manual/en/timezones.asia.php

America/Los_Angeles => America/Los_Angeles => PST
5

date_default_timezone_set('Asia/Ho_Chi_Minh').Asia/Ho_Chi_Minh').

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

ra đúng kết quả time ở Việt Nam.đú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().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.

Đô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).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 

America/Los_Angeles => America/Los_Angeles => PST
3 ).ú pháp:date_default_timezone_set ( string 
America/Los_Angeles => America/Los_Angeles => PST
3
 ).

tham số : $

America/Los_Angeles => America/Los_Angeles => PST
4http://php.net/manual/en/timezones.asia.php
America/Los_Angeles => America/Los_Angeles => PST
4http://php.net/manual/en/timezones.asia.php

America/Los_Angeles => America/Los_Angeles => PST
5

date_default_timezone_set('Asia/Ho_Chi_Minh').Asia/Ho_Chi_Minh').

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

ra đúng kết quả time ở Việt Nam.đú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().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.

Đô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).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 

America/Los_Angeles => America/Los_Angeles => PST
3 ).ú pháp:date_default_timezone_set ( string 
America/Los_Angeles => America/Los_Angeles => PST
3
 ).

tham số : $

America/Los_Angeles => America/Los_Angeles => PST
4http://php.net/manual/en/timezones.asia.php
America/Los_Angeles => America/Los_Angeles => PST
4http://php.net/manual/en/timezones.asia.php

America/Los_Angeles => America/Los_Angeles => PST
5

date_default_timezone_set('Asia/Ho_Chi_Minh').Asia/Ho_Chi_Minh').

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

ra đúng kết quả time ở Việt Nam.đú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().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.

Đô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).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 

America/Los_Angeles => America/Los_Angeles => PST
3 ).ú pháp:date_default_timezone_set ( string 
America/Los_Angeles => America/Los_Angeles => PST
3
 ).

tham số : $

America/Los_Angeles => America/Los_Angeles => PST
4http://php.net/manual/en/timezones.asia.php
America/Los_Angeles => America/Los_Angeles => PST
4http://php.net/manual/en/timezones.asia.php

America/Los_Angeles => America/Los_Angeles => PST
5

date_default_timezone_set('Asia/Ho_Chi_Minh').Asia/Ho_Chi_Minh').

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

ra đúng kết quả time ở Việt Nam.đú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().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.

date_timezone_set

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

DateTime::setTimezone -- date_timezone_set — Sets the time zone for the DateTime object -- date_timezone_setSets the time zone for the DateTime object

Parameters

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

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

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

A DateTimeZone object representing the desired time zone. 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. DateTime object for method chaining. The underlaying point-in-time is not changed when calling this method.

Examples

Example #1 DateTime::setTimeZone() exampleDateTime::setTimeZone() example

Object-oriented style

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

Procedural style

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

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.