Reputation: 6217
So, if I have a string like so: '2017-12-01T16:03:00' and need to convert this string into the timezone of America/New_York
, how to convert this string? Not exactly sure what the T
is doing in the string and what it's for exactly.
I'm curious on the correct method for this, while I believe the T
here is important, I understand the purpose of it. What I've tried here so far:
$string = '2017-12-01T16:03:00';
$time_fix = explode('T', $string);
if (count($time_fix) > 1)
{
$data_date = DateTime::createFromFormat('Y-m-d H:i:s', $time_fix[0] . ' ' . $time_fix[1]);
$output = $data_date->format('Y-m-d H:i:s', new DateTimeZone('America/New_York'));
} else {
$data_date = DateTime::createFromFormat('Y-m-d H:i:s', $time_fix[0] . ' 00:00:00');
$output = $data_date->format('Y-m-d H:i:s', new DateTimeZone('America/New_York'));
}
Not really sure if this is converting the timezone or not, so wondering on what your thoughts are on this? Will the $output
variable contain the date and time for the America/New_York
timezone? It is very difficult to test this as I don't know what timezone it is currently in when the time string gets created.
Also, wondering if the T here is important and what it means. And how to do this properly for the America/New_York
timezone?
UPDATE
Apparently, I found out that the Server Timezone that is creating these Date strings via the API is in Eastern Standard Time already. But if I didn't know this, how would it be converted to UTC? The developers of the API, states that it is UTC in 8601 format, but the timezone is EST. This does not make sense to me. Because, I believe EST is UTC-5, not just UTC. How the heck am I supposed to know this from what seems to be an improperly handled UTC timezone string result from the API? Am I correct in stating that 2017-12-01T16:03:00
is not the correct string for EST timezone from a UTC timezone?
Upvotes: 0
Views: 269
Reputation: 1194
If you do:
$date = new DateTime($info['CreateDate']);
$date->setTimeZone(new DateTimeZone('UTC'));
$date->setTimeZone(new DateTimeZone('America/New_York'));
$output = $date->format('Y-m-d H:i:s');
You are first creating the DateTime object in your default timezone which will affect the timestamp calculation, and then changing the timezone. Instead, assuming UTC input, you should do:
$date = new DateTime($info['CreateDate'], new DateTimeZone('UTC'));
$date->setTimeZone(new DateTimeZone('America/New_York'));
$output = $date->format('Y-m-d H:i:s');
This way the DateTime object will interpret the input date as UTC time and correctly compute the timestamp. Then setting the timezone is just to get the right date string, it does not affect the underlying timestamp.
I notice that ISO 8601 date strings can specify UTC offsets so in this particular case you may get away without specifying it as no offset is given. This will also make no difference in this case if your default timezone is UTC.
Edit based on new data:
If you know the input is in Eastern Standard Timezone try changing the line where you construct the DateTime object.
$date = new DateTime($info['CreateDate'], new DateTimeZone('EST'));
$date->setTimeZone(new DateTimeZone('America/New_York'));
$output = $date->format('Y-m-d H:i:s');
Upvotes: 1
Reputation: 156
Explode separate your $string
the elements on 2017-12-01
and 16:03:45
.
And here you have the answer Timezone conversion in php
Upvotes: 0