Date_Create Php Timezone


By David Mytton, CEO & Founder of Server Density. Published on the 22nd September, 2016.

Editor’s note: This is an updated version of an article originally published on 21/03/2009.

Bạn đang xem: Date_create php timezone

Baông xã in 2009 we introduced a new location preference feature for Server Density. Users could now specify their desired location, & then all dates/times automatically converted to their timezone (including handling of DST). We did that by using the DateTime class that was introduced with PHPhường 5.2.

Your very first challenge related to timezones is to giảm giá with how they are calculated relative sầu to the server’s mặc định timezone setting. Since PHP 5.1, all the date/time functions create times in the hệ thống timezone of the VPS. And as of PHPhường. 5.2 you can mix the timezone programmatically using the date_default_timezone_set() function.

So, if you Điện thoại tư vấn the date() function—without specifying a timestamp as the second parameter và the timezone is set to lớn GMT—then the date will default to lớn the +0000 timezone. Equally, if you set the timezone khổng lồ New York in winter time the timezone will be -0500 (-0400 in summer).

The ins & outs of handling timezone conversion

If you want the date in GMT, you need to lớn know the offphối of the date you’re working with so you can convert it khổng lồ +0000, if necessary. When would you need khổng lồ vì this? Well, the MySQL TIMESTAMP field type stores the timestamp internally, using GMT (UTC), but always returns it in the server’s timezone. So, for any SELECT statements you will need khổng lồ convert the timestamp you pass in your SQL to UTC.

This might sound complicated but you can let the DateTime class bởi most of the hard work. You first need to get the user lớn specify their timezone. This will be attached to any DateTime object you create so the right offmix can be calculated. The PHP.. manual provides a danh sách of all the acceptable timezone strings.

Xem thêm: Bí Quyết Giữ Da Luôn Tươi Trẻ Và Trì Hoãn Thời Gian Lão Hóa, 14 Cách Chăm Sóc Da Giúp Bạn Luôn Tươi Trẻ

There is also a PHP. function that outputs the danh mục of timezones. Server Density uses this to lớn generate a danh mục of timezones as a drop-down menu for the user to lớn select from.

DateTimeZone Object

Once you have sầu the user’s timezone, you can create a DateTimeZone object from it. This will be used for all the offmix calculations.

$userTimezone = new DateTimeZone($userSubmittedTimezoneString);

To convert a date/time inlớn the user’s timezone, you simply need lớn create it as a DateTime object:

$myDateTime = new DateTime("2016-03-21 13:14");

This will create a DateTime object which has the time specified. The parameter accepts any format supported by strtotime(). If you leave sầu it empty it will mặc định to lớn “now”.

cảnh báo that the time created will be in the default timezone of the hệ thống. This is relevant because the calculated offset will be relative sầu lớn that timezone. For example, if the hệ thống is on GMT và you want lớn convert lớn Paris time, it will require adding 1 hour. However, if the VPS is in the Paris timezone then the offset will be zero. You can force the timezone that you want $myDateTime khổng lồ be in by specifying the second parameter as a DateTimeZone object. If,  for example, you wanted it khổng lồ be 13:14 on 21st March 2016 in GMT, you’d need khổng lồ use this code or something similar:

$gmtTimezone = new DateTimeZone("GMT"); $myDateTime = new DateTime("2016-03-21 13:14", $gmtTimezone);

To double check, you can run:

emang đến $myDateTime->format("r");

which would output Mon, 21 Mar năm nhâm thìn 13:14:00 +0000.

The final step is to lớn work out the offmix from your DateTime object lớn the user’s timezone so you can convert it lớn that timezone. This is where the $userTimezone DateTimeZone object comes in (because we use the getOffset() method):

$offmix = $userTimezone->getOffset($myDateTime);

This will return the number of seconds you need to lớn add to $myDateTime to lớn convert it inkhổng lồ the user’s timezone. Therefore:

$userTimezone = new DateTimeZone("America/New_York"); $gmtTimezone = new DateTimeZone("GMT"); $myDateTime = new DateTime("2016-03-21 13:14", $gmtTimezone); $offset = $userTimezone->getOffset($myDateTime); emang đến $offset;

This will print -14400, or 4 hours (because New York is on DST).


As of PHP.. 5.3, you can also use DateTime::add method to create the new date just by adding the offmix. So:

$userTimezone = new DateTimeZone("America/New_York"); $gmtTimezone = new DateTimeZone("GMT"); $myDateTime = new DateTime("2016-03-21 13:14", $gmtTimezone); $offset = $userTimezone->getOffset($myDateTime); $myInterval=DateInterval::createFromDateString((string)$offset . "seconds"); $myDateTime->add($myInterval); $result = $myDateTime->format("Y-m-d H:i:s"); Emang đến $result;

The above would output 2016-03-21 09:14 which is the correct conversion from 2016-03-21 13:14 London GMT to Thành Phố New York time.

So that’s how we handle PHPhường timezones at Server Density. What’s your approach?


Free eBook: 4 Steps khổng lồ Successful DevOps

This eBook will show you how we i) hacked our on-điện thoại tư vấn rotation khổng lồ increase code resilience, ii) broke our infrastructure, on purpose, to lớn debug quicker & increase uptime, and iii) borrowed practices from the healthcare và aviation industry, to reduce complexity, ức chế & fatigue. And speaking of găng & fatigue, we’ve devoted an entire chapter on how we placed humans at the centre of Ops, in order khổng lồ increase their productivity and boost the uptime of the systems they manage. What are you waiting for, download your free copy now.
What vị you currently use for monitoring? I don"t use anything right now. SaaS monitoring. On-premise: commercial e.g. HP, IBM. On-premise: open source e.g. Nagquả táo. I’m reviewing it right now.

Help us speak your language. What is your primary tech stack?

Linux Windows OSX FreeBSD JVM MongoDB Cassandra Stavrou Redis SQL Server PostgreSquốc lộ MySquốc lộ Kafka RabbitMQ Apache NGINX IIS Ansible Puppet Saltstaông chồng Chef
1 - 24 servers 25 - 99 servers 100+ servers