Page 2
17 Sep
Admin

Transforming dates between time zones in PHP

Web Developers often face with time constraint. It sometimes is the productivity killer. Time zones are the ones that sometimes cause pain to the developers. Every developer faces this and then every QA team which tests the application is in the same time zone.

Yes, it is often that people from other time zone may use your app and this is the time that one must wait for.

Timezone in PHP

What is the default Time Zone?

Every developer has a baseline to work from and that is called the server time zone. Many people prefer to use UTC however something standard must be transformed and may get value to work from.

Whenever any new date is created without any time zone preference, it is then transformed into server’s time zone. From there, one can easily transform it to the users need. When you store the dates in the database which is based on server’s time zone then it becomes easy to relate the events with one another as this makes everything normal.

How to work with dates as objects?

With PHP, one can work with dates as objects or like an old procedural code. First of all, we will cover dates with objects. With the simple object oriented design it is very easy to transform date into objects.

Let’s consider this code:

echo 'America/Los_Angeles:
';
$date = new DateTime('2015-01-01', new DateTimeZone('America/Los_Angeles'));
echo $date->format('Y-m-d H:i:sP') . "

";

Here, we can see that a date time object in the Pacific time zone i.e.America Los Angeles is created. Now, below mentioned is another code which is written to transform this Pacific Time zone into Eastern Time zone.

echo 'America/New_York:
';
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format('Y-m-d H:i:sP') . "

";

In this code, we can see that the time zone which was created first is taken as object and now it will simply change to the time zone known as “American New York”. This sounds pretty easy, isn’t it?

So, what one must keep in mind is that while carrying out date conversion; one must know the source. It is very important to know the “Source”. Every date time function may not help you if in case you are unaware about the time zone of your data source.

While working with any applications when you are accepting any dates from the client side they will be mostly in the form of client’s time zone. Hence, it is very necessary to identify the time zone as it will normalize your data.

There are some applications that take care of this thing by knowing the users settings which show what the time zone is and very few depend solely on back end for the dates.

Wind Up

The main thing is after normalizing your inputs it is very easy to transform the time zone. One more time “Knowing your Inputs” is very important. So, next time when you wish to convert your time zone, keep your inputs handy.

For more such guidance, stay tuned with Softqube Technologies; PHP Web Development Company in India.

Share:
Admin
Hari Patel
He is the Managing Director of Softqube Technologies Pvt. Ltd., a modern-day digital transformation, design and development service provider. We provide services to businesses of all verticals across the globe. He believe and live by a mission that he help more entrepreneurs to build, launch and grow profitable businesses.
contact
Are you looking for real talent for your dream projects?
Inquiry
We will respond you in within 24 hours
Do you have a project in mind?
Let's start your project
Quick Contact
We can give you a fast response via email, phone call or skype.
For business inquiries
Call us on
Let's Skype
Close
Inquire
Let’s make something awesome together!
Drop us a line, or give us a heads up if you’re interested in visiting us.
  • Accepted file types: docx, xlsx, ppt, pdf, jpg, gif, png.
  • 9 − 4 =
  • This field is for validation purposes and should be left unchanged.
100% secure and private