Can I use NSISO8601DateFormatter to parse an ISO-8601 string? 0 Foundation in macOS 10.12 has a new class, NSISO8601DateFormatter, which claims to be able to parse ISO-8601, but I'm having trouble understanding how to use it. Java 8 Date and Time: parse ISO 8601 string without colon in offset (3) I wouldn't call it a solution but a workaround. SimpleDateFormat's Z template supports the timezone-syntax you showed, so you can do something like this.
![]() ![]()
$dt2- format( 'Y-m-dTH:i:s.uP');echo PHPEOL;$dt3 = DateTime:: createFromFormat( 'Y-m-dTH:m:i.uO', '1990-12-31T23:59:60Z'); // leap secondvardump( $dt3); // false$dt3 = new DateTime( '1990-12-31T23:59:60Z');echo '1990-12-31T23:59:60Z formatted = '. $dt3- format( 'Y-m-dTH:i:s.uP');echo PHPEOL;$dt4 = DateTime:: createFromFormat( DateTime:: ISO8601, '2004-02-13T15:19:21+00:00');echo '2004-02-13T15:19:21+00:00 formatted = '. $dt4- format( 'Y-m-dTH:i:s.uP');echo PHPEOL;$dt5 = DateTime:: createFromFormat( 'Y-m-dTH:m:i.uO', '1985-04-12T23:20:50.52Z');echo '1985-04-12T23:20:50.52Z formatted = '. $dt5- format( 'Y-m-dTH:i:s.uP');echo PHPEOL;echo 'DateTime::createFromFormat(DateTime::ISO8601, '1985-04-12T23:20:50.52Z') = ';$datetime = DateTime:: createFromFormat( DateTime:: ISO8601, '1985-04-12T23:20:50.52Z');vardump( $datetime);echo 'new DateTime('1985-04-12T23:20:50.52Z') = ';$datetime = new DateTime( '1985-04-12T23:20:50.52Z');vardump( $datetime);echo '1985-04-12T23:20:50.52Z formatted (P) = '. $datetime- format( 'Y-m-dTH:i:s.uP');echo PHPEOL;//$datetime = DateTime::createFromFormat('c', '1985-04-12T23:20:50.52Z'); // fails$datetime = new DateTime( '1985-04-12T23:20:50.52Z');vardump( $datetime);$datetimez = clone $datetime;$datetime- setTimeZone( new DateTimeZone( 'Zulu'));$datetime- setTimeZone( new DateTimeZone( 'Etc/UTC'));$datetime- setTimeZone( new DateTimeZone( 'UTC'));echo '1985-04-12T23:20:50.52Z clone Z formatted = '.
$datetime- format( 'Y-m-dTH:i:s.uZ');echo PHPEOL;echo '1985-04-12T23:20:50.52Z clone Z formatted = '. $datetime- format( 'Y-m-dTH:i:s.uP');echo PHPEOL;$iso8601p = sprintf('%s%03d%s',$datetime- format( 'Y-m-dTH:i:s.' ),floor( $datetime- format( 'u') / 1000),$datetime- format( 'P'));echo '1985-04-12T23:20:50.52Z clone custom formatted P = '. $iso8601p;echo PHPEOL;$iso8601z = sprintf('%s%03dZ',$datetime- format( 'Y-m-dTH:i:s.'
![]()
),floor( $datetime- format( 'u') / 1000));echo '1985-04-12T23:20:50.52Z clone custom formatted Z = '. $iso8601z;echo PHPEOL;echo 'DateTime::diff with fractions of a second granularity is not possible';echo PHPEOL;echo 'Propably need to use format('u') and compare those when treated as integer';echo PHPEOL;echo 'NOW with fractions (P): '. DateTime:: createFromFormat( 'U.u', sprintf( '%.6F', microtime( true)))- format( 'Y-m-dTH:i:s.uP');echo PHPEOL;echo 'NOW with fractions (O): '. DateTime:: createFromFormat( 'U.u', sprintf( '%.6F', microtime( true)))- format( 'Y-m-dTH:i:s.uO');echo PHPEOL.
![]() Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
March 2023
Categories |