Solved

Convert a GMT TimeStamp to Local Time using PHP

Posted on 2016-11-09
3
52 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 109

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 109

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 count occurrences of each item in an array.

792 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