Hướng dẫn dùng time zines 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].

Nếu chưa biết hàm date[format,timestamp] bạn có thể xem ở link://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://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.

i need to convert the given Australian times to UTC. this time can be from different states in AUS. i have the states in the DB and stored the time offset in a column.

Nội dung chính

  • How can I get timezone in PHP?
  • How can I modify the PHP timezone setting for my website?
  • How do you add time zones in HTML?
  • What is the default time zone in PHP?
  • Not the answer you're looking for? Browse other questions tagged javascript php or ask your own question.
  • Description
  • Return Values
  • What is my PHP time zone?
  • How do I change the default timezone in PHP?
  • How do I set default timezone?
  • What is default timezone?

Nội dung chính

  • How can I get timezone in PHP?
  • How can I modify the PHP timezone setting for my website?
  • How do you add time zones in HTML?
  • What is the default time zone in PHP?

i want to know how is possible to add a time offset or subtract it this way.

// stored values of states and its column.

"NT" = +9.30
"QLD" = +10.00
"WA" = +8.00

i am taking selected date time this way and need to reduce the offset.

$timeAinUTC = date["H:i A", strtotime["07am"]] - [offset]; 

i.e $timeAinUTC = date["H:i A", strtotime["07am"]] - [+9.30];

how can i get this kind of a work done with php's datetime ?

EDIT 1,

I tried this to add my offeset but it seems like i cannot provide double values like 2.30 hours or 2.50

It works when i ask it to add 2 hours this way.

strtotime[$current_time . "+2hours"];

but when i add like below , its wrong.

strtotime[$current_time . "+2.30hours"];

EDIT 2

All the sates available in AUS. ACT, NSW, NT, QLD, SA, TAS, VIC, WA

//wwp.greenwichmeantime.com/time-zone/australia/time-zones/

EDIT 3

I tried this as below as explained by a person in the answer.

$date = new DateTime['2013-10-01 01:45', new DateTimeZone['Australia/Melbourne']];
echo $date->format['Y-m-d h:i:s A'] . "
"; $date->setTimezone[new DateTimeZone['UTC']]; echo $date->format['Y-m-d h:i:s A'] . "
";

But it outputs,

2013-10-01 01:45:00 AM
2013-09-30 03:45:00 PM

How can this be wrong ? 2013-09-30 03:45:00 PM is wrong as per my understaing and actually it should be 10 hours behind from 2013-10-01 01:45:00 AM to be UTC, isnt it ?

How can I get timezone in PHP?

The date_default_timezone_get[] function returns the default timezone used by all date/time functions in the script.

How can I modify the PHP timezone setting for my website?

Open Hosting → Manage → PHP Configuration page. There, open the PHP options tab and edit the date. timezone value: If you are not sure which time zone to insert, check the Time Zone Map.

How do you add time zones in HTML?

You can use moment-timezone:.

Get list of time zones using moment. tz. names[].

Get the zone object for each time zone by using moment. tz. zone[name].

What is the default time zone in PHP?

Reading the timezone set using the date_default_timezone_set[] function [if any] Reading the value of the date. timezone ini option [if set] If none of the above succeed, the default timezone will be UTC.

I need to display local time in city New Your for everyone user, who visit my web site. How I can do it?

Nội dung chính

  • Not the answer you're looking for? Browse other questions tagged javascript php or ask your own question.
  • Description
  • Return Values
  • What is my PHP time zone?
  • How do I change the default timezone in PHP?
  • How do I set default timezone?
  • What is default timezone?

Nội dung chính

  • Not the answer you're looking for? Browse other questions tagged javascript php or ask your own question.
  • Description
  • Return Values
  • What is my PHP time zone?
  • How do I change the default timezone in PHP?
  • How do I set default timezone?
  • What is default timezone?

Nội dung chính

  • Not the answer you're looking for? Browse other questions tagged javascript php or ask your own question.
  • Description
  • Return Values
  • What is my PHP time zone?
  • How do I change the default timezone in PHP?
  • How do I set default timezone?
  • What is default timezone?

asked Jan 21, 2015 at 15:58

1

Put this at the top of your php.

date_default_timezone_set["America/New_York"];

You can also do this.

$date = new DateTime[];
$date->setTimezone[new DateTimeZone['America/Detroit']];
$fdate = $date->format['Y-m-d H:i:s'];`

$fdate will print the appropriate time in new york with date.

All the timezones here - //php.net/manual/en/timezones.america.php

answered Jan 21, 2015 at 16:00

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

America/Adak America/Anchorage America/Anguilla America/Antigua America/Araguaina America/Argentina/Buenos_Aires America/Argentina/Catamarca America/Argentina/Cordoba America/Argentina/Jujuy America/Argentina/La_Rioja America/Argentina/Mendoza America/Argentina/Rio_Gallegos America/Argentina/Salta America/Argentina/San_Juan America/Argentina/San_Luis America/Argentina/Tucuman America/Argentina/Ushuaia America/Aruba America/Asuncion America/Atikokan America/Bahia America/Bahia_Banderas America/Barbados America/Belem America/Belize America/Blanc-Sablon America/Boa_Vista America/Bogota America/Boise America/Cambridge_Bay America/Campo_Grande America/Cancun America/Caracas America/Cayenne America/Cayman America/Chicago America/Chihuahua America/Costa_Rica America/Creston America/Cuiaba America/Curacao America/Danmarkshavn America/Dawson America/Dawson_Creek America/Denver America/Detroit America/Dominica America/Edmonton America/Eirunepe America/El_Salvador America/Fort_Nelson America/Fortaleza America/Glace_Bay America/Goose_Bay America/Grand_Turk America/Grenada America/Guadeloupe America/Guatemala America/Guayaquil America/Guyana America/Halifax America/Havana America/Hermosillo America/Indiana/Indianapolis America/Indiana/Knox America/Indiana/Marengo America/Indiana/Petersburg America/Indiana/Tell_City America/Indiana/Vevay America/Indiana/Vincennes America/Indiana/Winamac America/Inuvik America/Iqaluit America/Jamaica America/Juneau America/Kentucky/Louisville America/Kentucky/Monticello America/Kralendijk America/La_Paz America/Lima America/Los_Angeles America/Lower_Princes America/Maceio America/Managua America/Manaus America/Marigot America/Martinique America/Matamoros America/Mazatlan America/Menominee America/Merida America/Metlakatla America/Mexico_City America/Miquelon America/Moncton America/Monterrey America/Montevideo America/Montserrat America/Nassau America/New_York America/Nipigon America/Nome America/Noronha America/North_Dakota/Beulah America/North_Dakota/Center America/North_Dakota/New_Salem America/Nuuk America/Ojinaga America/Panama America/Pangnirtung America/Paramaribo America/Phoenix America/Port-au-Prince America/Port_of_Spain America/Porto_Velho America/Puerto_Rico America/Punta_Arenas America/Rainy_River America/Rankin_Inlet America/Recife America/Regina America/Resolute America/Rio_Branco America/Santarem America/Santiago America/Santo_Domingo America/Sao_Paulo America/Scoresbysund America/Sitka America/St_Barthelemy America/St_Johns America/St_Kitts America/St_Lucia America/St_Thomas America/St_Vincent America/Swift_Current America/Tegucigalpa America/Thule America/Thunder_Bay America/Tijuana America/Toronto America/Tortola America/Vancouver America/Whitehorse America/Winnipeg America/Yakutat America/Yellowknife  

[PHP 5 >= 5.1.0, PHP 7, PHP 8]

date_default_timezone_set Sets the default timezone used by all date/time functions in a script

Description

date_default_timezone_set[string $timezoneId]: bool

Instead of using this function to set the default timezone in your script, you can also use the INI setting date.timezone to set the default timezone.

Parameters

timezoneId

The timezone identifier, like UTC, Africa/Lagos, Asia/Hong_Kong, or Europe/Lisbon. The list of valid identifiers is available in the List of Supported Timezones.

Return Values

This function returns false if the timezoneId isn't valid, or true otherwise.

Examples

Example #1 Getting the default timezone

See Also

  • date_default_timezone_get[] - Gets the default timezone used by all date/time functions in a script
  • List of Supported Timezones

Pierre Gourlaouen

10 years ago

A simple method for conversation between two time zone.

What is my PHP time zone?

The default timezone for PHP is UTC regardless of your server's timezone. This is the timezone used by all PHP date/time functions in your scripts.

How do I change the default timezone in PHP?

8 Answers.

Go to your phpinfo[] page and search for Loaded Configuration File and open the php. ini file mentioned under that section..

Change the default timezone settings by adding your new timezone by modifying this line: date. timezone=Asia/Kolkata ..

Save the php. ... .

Restart the Apache server..

How do I set default timezone?

The date_default_timezone_set[] function sets the default timezone used by all date/time functions in the script.

What is default timezone?

The defaultTimeZone time zone is used by the app for date and time operations. You can set it to cause the app to run as if it were in a different time zone. Setting the defaultTimeZone property clears any value that was previously set.

Chủ Đề