I have this date-time stamp:
"2021-06-08T13:09:00.9796129Z"
And I cannot convert it in to a DateTime object using CreateFromFormat. I am using a JanePHP Normaliser so would prefer to try and solve this strictly using CreateFromFormat. I have tried the following:
$options = [
\DateTimeInterface::ATOM,
\DateTimeInterface::COOKIE,
\DateTimeInterface::ISO8601,
\DateTimeInterface::RFC822,
\DateTimeInterface::RFC850,
\DateTimeInterface::RFC1036,
\DateTimeInterface::RFC1123,
\DateTimeInterface::RFC7231,
\DateTimeInterface::RFC2822,
\DateTimeInterface::RFC3339,
\DateTimeInterface::RFC3339_EXTENDED,
\DateTimeInterface::RSS,
\DateTimeInterface::W3C,
'Y-m-dTH:i:s.uP',
'Y-m-dTH:i:s.P',
'Y-m-dTH:i:s.vP',
];
foreach ($options as $name) {
var_dump(\DateTime::createFromFormat($name, "2021-06-08T13:09:00.9796129Z"));
}
All result in:
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
What is the correct format?
You have a simple problem, and a more complex problem.
The simple problem is that to match a literal T, you need to escape it with a backslash. That ought to allow this pattern:
'Y-m-d\TH:i:s.uP'
(Year-month-day, literal T, hour:minute:second.microsecond, time zone)
The complex problem is that your timestamp has 7 decimal places in the seconds, giving a resolution of a tenth of a microsecond. That's why theu
specifier isn't matching:
u: Microseconds (up to six digits)
A workaround would be to use the ? specifier which matches any single byte:
'Y-m-d\TH:i:s.u?P'
(Year-month-day, literal T, hour:minute:second.microsecond, ignore a byte for the extra digit, time zone)
var_dump(\DateTime::createFromFormat('Y-m-d\TH:i:s.u?P', "2021-06-08T13:09:00.9796129Z"));
# object(DateTime)#1 (3) {
# ["date"]=>
# string(26) "2021-06-08 13:09:00.979612"
# ["timezone_type"]=>
# int(2)
# ["timezone"]=>
# string(1) "Z"
# }
You're not escaping the T, so it's attempting to use that as a placeholder (Timezone abbreviation). The correct format would be
'Y-m-d\TH:i:s.u\Z'
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.