Solved

Convert a GMT TimeStamp to Local Time using PHP

Posted on 2016-11-09
3
58 Views
Last Modified: 2016-11-23
I am doing an API call that gives me a type of timestamp in GMT.   For example, it comes in as 2016-11-09T12:18:15+00:00.

I want to convert this to the time of the server running the PHP program which is a GMT -5 and express it YYYY-MM-DD HH:mm:ss

So for the above time how do I convert 2016-11-09T12:18:15+00:00 to 2016-11-09 07:15:15.

Thanks.
0
Comment
Question by:pkonstan1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41880660
0
 

Author Comment

by:pkonstan1
ID: 41880821
Sorry I wasn't more clear. I don't want to set the time AS my local server time, I want to convert the API read time to my local time.

This API call happens in batch mode, so it reads four hours worth of data so I can't used the current time. I need to convert the time that comes to me as the GMT format into what the equivalent time was when the record was created.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41880925
Right - that's strtotime() and date().  The date_default_timezone is your local setting.  It has to be right for the date/time functions of PHP to consider your locale.  You can check it by running this script, shown here in its entirety, and looking for "date.timezone" in the output.
<?php phpinfo();

Open in new window


Then do something like this:
https://iconoun.com/demo/temp_pkonstan1.php
<?php // /demo/temp_pkonstan1.php
/**
 * https://www.experts-exchange.com/questions/28982010/Convert-a-GMT-TimeStamp-to-Local-Time-using-PHP.html
 */
error_reporting(E_ALL);


$gmt = '2016-11-09T12:18:15+00:00';
$fmt = 'Y-m-d H:i:s';

// PROCEDURAL
$loc = date($fmt, strtotime($gmt));
echo PHP_EOL . "GMT $gmt EQUALS LOCAL DATE/TIME $loc";

// OOP
$dto = new DateTime($gmt);
$dto->setTimeZone(new DateTimeZone( date_default_timezone_get() ));
$loc = $dto->format($fmt);
echo PHP_EOL . "GMT $gmt EQUALS LOCAL DATE/TIME $loc";

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question