So I"ve checked the danh mục of supported time zones in PHP & I was wondering how could I include them in the date() function? Thanks!
I don"t want a mặc định timezone, each user has their timezone stored in the database, I takethat timezone of the user và use it. How? I know how lớn take it from the database, not how khổng lồ use it, though.
Bạn đang xem: Thiết lập time zone trong php
11k2 gold badges54 silver badges57 bronze badges
asked Nov 29, 2013 at 15:15
For such task, you should really be using PHP"s DateTime class. Please ignore all of the answers advising you to use date() or date_set_time_zone, it"s simply bad and outdated.
I"lluse pseudocode to lớn demonstrate, so try to adjust the code to lớn suit your needs.
Assuming that variable $tz contains string name of a valid time zone và variable $timestamp contains the timestamp you wish lớn format according to lớn time zone, the code would look like this:
$tz = "Europe/London";$timestamp = time();$dt = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string$dt->setTimestamp($timestamp); //adjust the object lớn correct timestampecho $dt->format("d.m.Y, H:i:s");DateTime class is powerful, & to grasp all of its capabilities - you should devote some of your time reading about it at php.net. To lớn answer your question fully - yes, you can adjust the time zone parameterdynamically (on each iteration while reading from db, you can create a new DateTimeZone() object).
5,2357 gold badges32silver badges47 bronze badges
answered Nov 29, 2013 at 15:34
13.3k3 gold badges44 silver badges53 bronze badges
If I understood correct,You need lớn set time zone first like:
date_default_timezone_set("UTC");And than you can use date function:
// Prints something like: Monday 8th of August 2005 03:12:46 PMecho date("l jS of F Y h:i:s A");
6,6182 gold badges43 silver badges86 bronze badges
answered Nov 29, 2013 at 15:18
Suresh KamrushiSuresh Kamrushi
14.9k12 gold badges74 silver badges87 bronze badges
Use the DateTime class instead, as it supports timezones. TheDateTime equivalent of date() is DateTime::format.
An extremely helpful wrapper for DateTime is Carbon - definitely give it a look.
You"ll want to store in the database as UTC & convert on the application level.
Xem thêm: Zro2 Là Gì ? Zirconium Silicate
answered Nov 29, 2013 at 15:17
173k40 gold badges288 silver badges358 bronze badges
Theanswer above caused me lớn jump through some hoops/gotchas, so just posting the cleaner code that worked for me:
$dt = new DateTime();$dt->setTimezone(new DateTimeZone("America/New_York"));$dt->setTimestamp(123456789);echo $dt->format("F j, Y
answered Sep 13, 2017 at 18:28
16.9k11 gold badges95 silver badges107 bronze badges
It should like this:
answered Nov 29, 2013 at 15:21
U can justadd, timezone difference to lớn unix timestamp. Example for Moscow (UTC+3)
echo date("d.m.Y H:i:s", time() + 3 * 60 * 60);
answered Jun 23, 2020 at 19:10
this works perfectly in 2019:
date("Y-m-d H:i:s",strtotime($date. " ".$timezone));
answered Jul 13, 2019 at 6:57
Try this. You can pass either unix timestamp, or datetime string
public static function convertToTimezone($timestamp, $fromTimezone, $toTimezone, $format="Y-m-d H:i:s") $datetime = is_numeric($timestamp) ? DateTime::createFromFormat ("U" , $timestamp, new DateTimeZone($fromTimezone)) : new DateTime($timestamp, new DateTimeZone($fromTimezone)); $datetime->setTimezone(new DateTimeZone($toTimezone)); return $datetime->format($format);
answered Oct 9, năm trước at 20:32
3,0453 goldbadges31 silver badges44 bronze badges
I have created this very straightforward function, and it works lượt thích a charm:
function ts2time($timestamp,$timezone) /* input: 1518404518,America/Los_Angeles */ $date = new DateTime(date("d F Y H:i:s",$timestamp)); $date->setTimezone(new DateTimeZone($timezone)); $rt=$date->format("M d, Y h:i:s a"); /* output: Feb 11, 2018 7:01:58 pm */ return $rt;
answeredFeb 12, 2018 at 3:06
I have tried the answers based on the DateTime class. While they are working, I found a much simpler solution that makes a DateTime object timezone aware at the time of creation.
$dt = new DateTime("now", new DateTimeZone("Asia/Jakarta"));echo $dt->format("Y-m-d H:i:s");This returns the current local time in Jakarta, Indonesia.
answered Feb 7 at 19:15
Not mentioned above. You could also crate a DateTime object by providing a timestamp as string in the constructor with a leading
$dt = new DateTime("
answeredSep 22, 2020 at 10:10
611 silver badge2 bronze badges
If you use Team EJ"s answer, using Tin the format string for DateTime will display a three-letter abbreviation, but you can get the long name of the timezone lượt thích this:
answered Dec 27, 2021 at 0:02
Bob RayBob Ray
9089 silver badges20 bronze badges
$now = new DateTime();$now->format("d-m-Y H:i:s T")Will output:
answered Dec 29, 2021 at 12:24
Based on other answers I built a one-liner, where I suppose you need current date time. It"s easy to lớn adjust if you need a different timestamp.
$dt = (new DateTime("now", new DateTimeZone("Europe/Rome")))->format("d-m-Y_His");
answeredApr trăng tròn at 13:43
6636 silver badges12 bronze badges
I had a weird problem on a hosting. The timezone was phối correctly, when I checked it withthe following code.
echo ini_get("date.timezone");However, the time it returned was UTC.
The solution was using the following code since the timezone was set correctly in the PHP configuration.
answered May 25 at 7:59
B. MartinB. Martin
1,00010 silver badges17 bronze badges
You can replace database value in date_default_timezone_set function, date_default_timezone_set(SOME_PHP_VARIABLE); but just needs to take care of exact values relevant khổng lồ the timezones.