相关文章推荐
博学的地瓜  ·  Matplotlib ...·  11 月前    · 
低调的警车  ·  WPF ...·  1 年前    · 
无聊的松鼠  ·  android 11 open ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams @Gordon I'm still learning how to write from A to Z but I found they teach me how to write from Z to A :) wow Mar 16, 2011 at 9:18 @wow No. They teach you how to write alphabet($from, $to). Almost all of them tell you to use date($format, strtotime($dateString)) or the DateTime object equivalent. They only differ in the input to those functions. All you have to do is go to the correponding pages in the PHP Manual and find out what the input is. Gordon Mar 16, 2011 at 9:28 @Gordon Yes are you correct. I just knew the alphabet and now spelling bee time. Hope I'm in the right school. wow Mar 16, 2011 at 9:46

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'));
                Question, the output is 2010-12-30T23:21:46+1100 how to make it to be 2010-12-30T23:21:46+11:00?
– wow
                Mar 16, 2011 at 8:53
                I would note that using DATE_ISO8601 produces a date string which is slightly different than ISO8601 (the colon is missing in the TZ, ISO8601 expects times to be all with OR all without the colon, not a mixture) - date('c') does produces a strict ISO 8601 valid date - This could cause hard to trace bugs if code expects a strict ISO 8601 datetime format. Ref: en.wikipedia.org/wiki/ISO_8601
– ckm
                Mar 23, 2015 at 1:42
                @alex, for some reason the constant ISO8601 is not compatible with ISO-8601, use DateTime::ATOM instead, see the note in php.net/manual/en/class.datetime.php#datetime.constants.iso8601
– Guilherme
                Sep 17, 2015 at 2:49

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.

@rybo111 I don't see a reason to down-vote this answer; technically, both 'c' and DATE_ISO8601 produce valid ISO8601 time representations. – Ja͢ck Jun 4, 2015 at 3:24 @Ja͢ck DATE_ISO8601 does not produce a valid ISO8601 representation. DATE_ATOM does though – Captain Obvious Jun 21, 2018 at 18:22

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

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

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

$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 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
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.