Solved

Convert a GMT TimeStamp to Local Time using PHP

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

724 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