RFC 3339 how make a dateTime from

后端 未结 2 1358
南旧
南旧 2021-01-17 18:27

I\'m trying to format a date passed from a google plus Api thats like the guide says in RFC 3339 format:

PUBLISHED-> datetime-> The time at which t

相关标签:
2条回答
  • 2021-01-17 19:02

    You don't need to use DateTime::createFromFormat() for standard inputs. Just use:

    $date = new DateTime('2014-01-22T10:36:00.222Z');
    var_dump($date);
    

    But if you still insist to use createFromFormat(), then use correct format, with microseconds:

    $date = DateTime::createFromFormat('Y-m-d\TH:i:s.uP', '2014-01-22T10:36:00.222Z');
    var_dump($date);
    
    0 讨论(0)
  • 2021-01-17 19:16

    There is a trick. A special constant DATE_RFC3339 was made to help, but it does not work if the last character is "Z" - which is perfectly fine for rfc3339 format. Actually JSON would specify format like that:

    expected format YYYY-MM-DDThh:mm:ssZ or YYYY-MM-DDThh:mm:ss+hh:mm
    

    But using this DATE_RFC3339 you can receive an Error message from PHP:

    InvalidArgumentException: The timezone could not be found in the database
    

    That is why we need to specify format manually:

    With DateTime

    $date = DateTime::createFromFormat ( 'Y-m-d\TH:i:s.u\Z' ,$time);
    

    With Carbon:

    \Carbon\Carbon::createFromFormat('Y-m-d\TH:i:s.u\Z',$time);
    
    0 讨论(0)
提交回复
热议问题