I’m trying to format a given zulu time but it returns a wrong date and time.
$yourDate = "2024-07-16T11:29:32.347043421Z";
echo date('Y-m-d h:i:s', strtotime($yourDate));
Above will return 1970-01-01 12:00:00
. But it returns correct if there are only 8 decimal palaces. The below script works but I don’t know for other values.
$yourDate = "2024-07-16T11:29:32.347043421Z";
$trimmedDate = substr($yourDate, 0, 26) . "Z";
echo date('Y-m-d H:i:s', strtotime($trimmedDate));
4
Online Demo Here : https://onecompiler.com/php/42kertuvz
PHP Version 7.3 Demo : https://onlinephp.io/c/55c29
<?php
$yourDate = "2024-07-16T11:29:32.347043421Z";
// Remove the Z (Zulu time indicator) and extra nanoseconds
$yourDate = substr($yourDate, 0, 23) . 'Z';
// Create a DateTime object from the string
$date = new DateTime($yourDate, new DateTimeZone('UTC'));
// Format the date and time as You needed
$formattedDate = $date->format('Y-m-d H:i:s.u');
echo $formattedDate;
?>