get the ISO 8601 with seconds.decimal-fraction-of-second date in php?

前端 未结 5 1014
遇见更好的自我
遇见更好的自我 2020-12-16 19:26

I echo this :

  php> echo date(\"Y-m-d\\TH:i:s\");
      2011-05-27T11:21:23

How can do with date function to get this date format:

5条回答
  •  天涯浪人
    2020-12-16 19:31

    As a solution for 2020 with the DateTime class, which was added in PHP 5.2, you can do a simple one liner to get the wanted format.

    For example:

    echo (new DateTime())->format('Y-m-d\TH:i:s.uP');
    // 2020-04-23T09:18:25.311075+02:00
    

    The DateTimeInterface, which is implemented by the DateTime class, brings its own constants for widely used date formats. If you know the format, you can use a constant for that.

    echo var_dump($datetime->format(DateTime::RFC3339_EXTENDED));
    // 2020-04-23T09:18:25.311+02:00
    

    As object orientated programming is widely spread in the PHP world, this could be a possible solution, too.

提交回复
热议问题