Convert DateTime to ISO 8601 DateTime Format with timezone - php

How do I convert my time from 2010-12-30 23:21:46 to ISO 8601 date format?
I tried $datetime->format(DateTime::ATOM) and date(DATE_ISO8601, strtotime('2010-12-30 23:21:46')) which converts into ISO8601 format like 2016-12-31T09:47:50+0000. But what i need is 2018-02-19T05:43:59.753000Z format. How can i achieve using PHP??

If you wish to output format in Zulu/UTC/GMT timezone, then you must first convert timezone, since I do not know in which timezone is your current setup.
$dt = new DateTime('2010-12-30 23:21:46');
$dt->setTimezone(new DateTimeZone('UTC'));
echo $dt->format('Y-m-d\TH:i:s.u\Z');
demo
And ISO-8601 is a standard that has many variations of format:
2018-04-03
2018-04-03T05:20:03+00:00
2018-04-03T05:20:03Z
20180403T052003Z
2018-W14
2018-W14-2
...
This are all valid ISO-8601 formats. Read more here.

Related

How to convert MySQL date time into ISO 8601 date format without offset?

I have following MySQL datetime:
2012-04-13 09:45:36
I am converting it into ISO 8601 date format using
date(DATE_ISO8601, strtotime('2012-04-13 09:45:36'));
It gives me following output:
2012-04-13T09:45:36+0100
How can I get output without offset i.e. without +0100
I just want to get 2012-04-13T09:45:36
Can datetime be affected if I get it after removing the offset?
You have to build the date format with the formatting parameter you need.
date(DATE_ISO8601, strtotime('2012-04-13 09:45:36')); is the same as date('Y-m-d\TH:i:sO', strtotime('2012-04-13 09:45:36'));. If you remove the O parameter (offset to GMT) you will get the result you need:
date('Y-m-d\TH:i:s', strtotime('2012-04-13 09:45:36'));

PHP ISO 8601 format conversion issue

I want to convert this string '2016-05-30 15:35:00' into ISO 8601 format which should look like this:
Expected result : 2016-05-30T15:35:00+05:30
I tried following, but in the output it's 2 hours less,
$phpdate = strtotime('2016-05-30 15:35:00');
echo gmdate('Y-m-d\TH:i:s\+05:30', $phpdate);
Output : 2016-05-30T13:35:00+05:30
Have I made a mistake somewhere? or is there an easier way for this conversion?
You're using gmdate function of PHP which converts date into UTC format instead you need to use date function like as
date_default_timezone_set("Asia/Kolkata");//Set your timezone to Asia/Kolkata
$phpdate = strtotime('2016-05-30 15:35:00');
echo date('c', $phpdate);// 2016-05-30T13:35:00+05:30 if your timezone is of Asia/Kolkata
for an easier way to handle dates i recommend Carbon
for your problem it would be :
$date = new Carbon("2016-05-30 15:35:00");
echo $date->toIso8601String();

Convert Date in PHP to XMLGregorianCalendar format and back

I'm calling a SOAP web service that expects to receive a date/time in XMLGregorianCalendar format. So, for example, I would need to convert YYYY-MM-DD HH:MM:SS to that format.
When I retrieve data from the web service, I would need to convert date/time from XMLGregorianCalendar format to YYYY-MM-DD HH:MM:SS.
I would normally post some code that I have tried but, in all honesty, I'm having trouble locating anything to even get me started with PHP. So my apologies for the lack of sample code. Would REALLY appreciate any guidance.
The date format used in XML is ISO-8601. You can use strftime to convert it to a Unix timestamp and date to convert it back to a nice readably sting.
These format string might come handy:
"Y-m-d H:i:s" sample: "2014-06-27 20:14:49"
"c" sample: "2014-06-27T20:14:49+00:00"
You can check supported time formats of strtotime here:
http://de3.php.net/manual/en/function.strtotime.php
And syntax for date here:
http://www.php.net/manual/en/function.date.php
$date = date("Y-m-d H:i:s", strtotime($date)); // Output as datetime used in SQL
$date = date("c", strtotime($date)); // Output as datetime used in XML

How to convert yyyy-MM-ddTHH:mm:ssZ to yyyy-MM-dd HH:mm:ss?

Paypal returns a timestamp of the following format:
yyyy-MM-ddTHH:mm:ssZ
And I don't quite know what to do with it...
How can I convert it to yyyy-MM-dd HH:mm:ss using my local timezone in php?
I'm tempted to preg_replace the mysterious letters, but something tells me there must a better way. There also appears to be 8 hours difference to my zone which I'm not sure how to substract.
Use DateTime class to do your magic.
$date = new DateTime('2012-09-09T21:24:34Z');
$date->format('Y-m-d'); # read format from date() function
You can use strtotime() to get a UNIX timestamp. From there you can do whatever you need: DateTime object, date(), etc.
Example with date():
echo date('r', strtotime('2012-09-10T10:00:00Z'));

How do I convert datetime to ISO 8601 in PHP

How do I convert my time from 2010-12-30 23:21:46 to ISO 8601 date format? (-_-;)
Object Oriented
This is the recommended way.
$datetime = new DateTime('2010-12-30 23:21:46');
echo $datetime->format(DateTime::ATOM); // Updated ISO8601
Procedural
For older versions of PHP, or if you are more comfortable with procedural code.
echo date(DATE_ISO8601, strtotime('2010-12-30 23:21:46'));
After PHP 5 you can use this: echo date("c"); form ISO 8601 formatted datetime.
http://ideone.com/nD7piL
Note for comments:
Regarding to this, both of these expressions are valid for timezone, for basic format: ±[hh]:[mm], ±[hh][mm], or ±[hh].
But note that, +0X:00 is correct, and +0X00 is incorrect for extended usage. So it's better to use date("c"). A similar discussion here.
How to convert from ISO 8601 to unixtimestamp :
strtotime('2012-01-18T11:45:00+01:00');
// Output : 1326883500
How to convert from unixtimestamp to ISO 8601 (timezone server) :
date_format(date_timestamp_set(new DateTime(), 1326883500), 'c');
// Output : 2012-01-18T11:45:00+01:00
How to convert from unixtimestamp to ISO 8601 (GMT) :
date_format(date_create('#'. 1326883500), 'c') . "\n";
// Output : 2012-01-18T10:45:00+00:00
How to convert from unixtimestamp to ISO 8601 (custom timezone) :
date_format(date_timestamp_set(new DateTime(), 1326883500)->setTimezone(new DateTimeZone('America/New_York')), 'c');
// Output : 2012-01-18T05:45:00-05:00
If you try set a value in datetime-local
date("Y-m-d\TH:i",strtotime('2010-12-30 23:21:46'));
//output : 2010-12-30T23:21
According to PHP offcial documentation you can simply format it to:
echo $objDateTime->format('c'); // ISO8601 formated datetime
echo $objDateTime->format(DateTime::ISO8601); // Another way to get an ISO8601 formatted string
ISO 8601 is basically represented in PHP as "Y-m-d\TH:i:sP"
You can get this value from a constant:
DateTime::ATOM - for PHP versions below 7.2 (was removed)
DateTimeInterface::ATOM - for PHP versions since 7.2
$datetime->format('Y-m-d\TH:i:s.u\Z') should give the proper format, with the "T" separator, "Z" timezone (make sure to convert to UTC first) and microseconds (omit .u if you don't intend to support fractional seconds).
See https://stackoverflow.com/a/9532375/65387 for discussion why should use T
You can try this way:
$datetime = new DateTime('2010-12-30 23:21:46');
echo $datetime->format(DATE_ATOM);
You can also get your timestamps conversion via mutation inside modal like this
class YourModal extends Model
{
public function getCreatedAtAttribute($date)
{
return date(DATE_ISO8601, strtotime($date)); // ISO 8601 Date Format
}
}

Categories