Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Convert a GMT TimeStamp to Local Time using PHP

Posted on 2016-11-09
3
Medium Priority
?
118 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
 

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
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…
Suggested Courses

618 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