Solved

Convert a GMT TimeStamp to Local Time using PHP

Posted on 2016-11-09
3
18 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
  • 2
3 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
0
 

Author Comment

by:pkonstan1
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now