I have date
$timeZome = timezone_open('Europe/Kiev');
$date = new DateTime();
$date->setTimezone($timeZome);
$date->setDate(2011, 06,25);
$date->setTime(11,35,00);
How to present like that?
20110625T040000Z
This will do what you want:
$date = new DateTime();
$date->setTimezone(new DateTimeZone('Europe/Kiev'));
$date->setDate(2011, 06,25);
$date->setTime(11,35,00);
$date->setTimezone(new DateTimeZone('UTC'));
echo $date->format('Ymd\THis\Z'); // format string to match question
It's pretty straightforward: set the date/time/zone, modify the timezone and format for printing.
You may just need to call format on your date object, I think.
echo $date->format('Ymd'); // 20110625
UPDATE:
You may want to look into either using the existing constants in the date class, or if your needs differ from what date offers, simply extend it and add a method that parses and returns your date formatted as you want it to.
Related
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?
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');
Explode separate your $string the elements on 2017-12-01 and 16:03:45.
And here you have the answer Timezone conversion in php
I am getting time in the following format as a response of an API call. How I can I convert it to Y-m-d H:i:s I used the following :
$time_other_format = '2013-10-29T08:34:01-0700';
$time = date("Y-m-d H:i:s",strtotime($time_other_format));
This is returning the time, but the time is changed and I think as per the timezone. I have records in DB which are inserted before the convesrion and so retain the original time and to compare it. How can I convert it like that . I mean I need to get the time as 08:34:01 after converting it
$time_other_format = '2013-10-29T08:34:01-0700';
$dt = new DateTime($time_other_format);
echo $dt->format('Y-m-d H:i:s');
This will echo,
2013-10-29 08:34:01
The DateTime class in PHP is very powerful, and will recognise almost any format you throw at it. It's great for manipulation.
I am trying to have a line of code added to an html document that is preceded by the time. I want the time zone to be relative to me, however I cannot change it from the default UTC. I have changed in the php.ini file to PST as well as using date_default_timezone_set('America/Los_Angeles'); and yet it still prints the time 7 hours ahead of my timezone. Heres the code that deals with the time:
session_start();
if(isset($_SESSION['name']))
{
date_default_timezone_set('America/Los_Angeles');
$msg = $_POST['text'];
$fo = fopen("log.html", 'a');
fwrite($fo, "<div class=msgln>(".date("g:i A").") <b style=color:red;>".$_SESSION['name']."</b>: ".stripslashes(htmlspecialchars($msg))."<br></div>
");
fclose($fo);
}
Servers should be set to UTC, and you should not be looking to change the default. Instead, what you want to do is create a DateTime object based on the time, then convert it to the timezone you want, and display.
$now = new DateTime();
$now->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo $now->format('g:i A');
I don't know if your format string is valid or not, but the format method is suppossed to be compatible with that accepted by the date() function you were using in your original example.
First make sure you're using a valued timezone. You can find a list of supported timezones in the PHP docs.
The second problem is using date() without specifying the timestamp. This defaults to the timestamp produced by time() which (based on a comment in the documentation) is UTC time. You'll either have to use strftime() or manually subtract the difference from UTC.
If you use 'etc/GMT' you can set the dateTime object to the desired time zone like so:
$dtz = new DateTimeZone('etc/GMT-10');
$dt = new DateTime(date("Y-m-d h:i A"), $dtz);
$date = gmdate("Y-m-d h:i A", $dt->format('U'));
Is there a php func that does this:
$timestamp = get_timestamp_from_str('d/m/Y H:i', '10/10/2012 10:10');
strtotime() will use the USA version for '10/10/2012' wich is m/d/Y, but i have the day first.
For a particular case i can make my own parser, but the date format can change depending on the visitors local settings. However, i will always know the current format.
I had to insert this last paragraph so that this question is long and good enough for this portals question quality filter
Thank you
Yes, it's called DateTime::createFromFormat().
You would use it as:
$datetime = DateTime::createFromFormat( 'd/m/Y H:i', '10/10/2012 10:10', new DateTimeZone('Something'));
$timestamp = $datetime->getTimestamp();
Just make sure your timezone string is in the list of supported timezones.
I have a date stored in a database in this format:
2011-02-23 13:00:00
I need to return it in ISO8601 format, but it needs to be set to a specific time zone (which is not necessarily the time zone of the server.) What I want to return is this:
2011-02-23T13:00:00-0600
Using this code:
echo date(DATE_ISO8601, strtotime("2011-02-23 13:00:00"));
I get this:
2011-02-23T13:00:00+0000
Is there any way to reset the time zone in the date or strtotime function, or do I need to strip off the 5 rightmost characters and concatenate the desired timezone stamp to the remaining date/time?
EDITED TO ADD:
Although I did accept the solution below of using new DateTime and setting new DateTimeZone, I found an easier way if you don't need to keep resetting the time zone:
date_default_timezone_set('America/Chicago');
$startTime = date(DATE_ISO8601, strtotime("2011-02-23 13:00:00"));
You could use the DateTime class. Datetime objects can be initialized with a specific time zone, and easily transposed to others.
Modified from the manual:
$date = new DateTime('2011-02-23 13:00:00', new DateTimeZone('Pacific/Nauru'));
echo $date->format('c') . "\n";
$date->setTimezone( new DateTimeZone('Europe/Berlin'));
echo $date->format('c') . "\n";