How to display a date as iso 8601 format with PHP
The second argument of date
is a UNIX timestamp, not a database timestamp string.
You need to convert your database timestamp with strtotime.
<?= date("c", strtotime($post[3])) ?>
How do I convert datetime to ISO 8601 in PHP
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'));
How to get timestamp in ISO 8601 format with timezone in php
Considering the Wikipedia Article on ISO_8601 the UTC Offset can be defined as a Hour:Minutes Definition of as a HoursMinutes Definition.
Z is the zone designator for the zero UTC offset. "09:30 UTC" is therefore represented as "09:30Z" or "0930Z". "14:45:15 UTC" would be "14:45:15Z" or "144515Z".
The PHP date method defines the parameter Z as
Z Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.
So assuming the offset mentioned in the wikipedia article is in seconds, you could create your own ISO 8601
using date. Example given for current server time/date:
date('Y-m-d\TH:i:s.Z\Z', time());
Also, as mentioned in the comments by @AndrewIsOffline, since PHP5, using 'c' will also give you the ISO 8601 Date:
date('c', time());
How to convert MySQL date time into ISO 8601 date format without 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'));
Convert Time to ISO 8601 format in UTC timezone
If you want the ISO 8601 format you should use the DateTimeInterface::ISO8601
in the format method, or you can use "c"
:
$date = new DateTime("Fri, 15 Mar 2019 08:56:57 +0000");
echo $date->format(\DateTime::ISO8601);
// will be 2019-03-15T08:56:57+0000
echo $date->format("c");
/*
will be 2019-03-15T08:56:57+00:00
note the : in between hh and mm in offset(timezone)
generally accepted as valid ISO 8061 see:
https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC
*/
Regarding the timezone if you want to force it into UCT timezone then you should use the setTimezone
method on the date object first with timezone param "UTC"
:
$date = new DateTime("Fri, 15 Mar 2019 08:56:57 +0000");
$date->setTimezone(new DateTimeZone("UTC"));
$output = $date->format(\DateTime::ISO8601);
Note about the above that if your original date time is not in UTC(has an offset) the time will be converted to UTC and the offset will be 0000:
$date = new DateTime("Fri, 15 Mar 2019 08:56:57 +0230");
echo $date->format(\DateTime::ISO8601);
// will be: 2019-03-15T08:56:57+0230
$date->setTimezone(new DateTimeZone("UTC"));
echo $date->format(\DateTime::ISO8601);
// will be: 2019-03-15T06:26:57+0000
Convert DateTime to ISO 8601 DateTime Format with timezone
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.
How to convert ISO8601 to Date format in php
try this
$date = '2014-03-13T09:05:50.240Z';
$fixed = date('Y-m-d', strtotime($date));
The complete date function documentation can be found here: http://php.net/manual/en/function.date.php
The PHP function "strtotime" does nothing else then converting your timestring into an unix timestamp.
Hope I could help :)
P.s.:
Just in case strtotime will return 0 try using this:
$date = '2014-03-13T09:05:50.240Z';
$fixed = date('Y-m-d', strtotime(substr($date,0,10)));
Related Topics
How to Convert Soap Response to PHP Array
Scandir() to Sort by Date Modified
How to Check If an Integer Is Within a Range
Uncaught Reflectionexception: Class Log Does Not Exist Laravel 5.2
How to Generate Unique Id in MySQL
How Add Class='Active' to HTML Menu with PHP
PHP Warning: Module Already Loaded in Unknown on Line 0
How to Tell If a Session Is Active
Select Outgoing Ip for Curl Request
How to Setup Table Prefix in Symfony2
How to Display a Date as Iso 8601 Format with PHP
What Does a Successful MySQL Delete Return? How to Check If Delete Was Successful
How to Sort Rows of HTML Table That Are Called from MySQL
What Does Filter_Sanitize_String Do