It's a ISO 8601 datetime string with microseconds, where Z
is the timezone "Zulu" or UTC + 0 hours
.
ISO 8601 can be parsed with DateTime()
like this:
$string = '2022-09-30T21:39:25.220185Z';
//create DateTime object
$date = date_create_from_format( "Y-m-d\TH:i:s.uP" , $string);
echo $date->format( 'Y-m-d H:i:s.u' );
However This will not work with your string, as the u
parameter in the format "Y-m-d\TH:i:s.uP"
which represents the microseconds, in PHP takes a maximum of 6 digits
, and yours has 9
.
You can resolve this by removing all above 6 digits from the microseconds part of the string with a regex, like
$string = '2022-09-30T21:39:25.220185674Z';
$new_string = preg_replace( '/^.*?\.\d{0,6}\K\d*/' , '' , $string );
$date = date_create_from_format( "Y-m-d\TH:i:s.uP" , $new_string );
echo $date->format('Y-m-d H:i:s.u');
Output: 2022-09-30 21:39:25.220180
The regex explained:
1. ^.*?\.\d{0,6} // select from the begin everything including the dot
// and max 6 digits
2. \K // forget the previous match and start again from the
// point where 1. ended
3. \d* // select all digits left
4. replace the match with ""