Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ronster's avatar

parse date with nanoseconds

Hi,

I have a date in the following format "2016-10-07T23:59:59.9999999999".

How can I parse this to a valid DateTime instance? seems PHP can only handle a 6 digit microseconds. not a 9 digit nanoseconds.

0 likes
7 replies
bobbybouwmann's avatar

This should give you a DateTime like object. It uses the DateTimeInterface, which is used by DateTime as well

date_create_immutable('2016-09-26 11:12:13.123456');

Note: You probably need to parse the T our of this all

Ronster's avatar

that has microseconds, mine has nanoseconds which doesn't parse.

tomopongrac's avatar

I don't know is it right way to do but you can remove last 3 digits from string

Ronster's avatar

that's indeed what i'm doing now! however it doesn't feel solid.

tomopongrac's avatar

I can't see other solution because php can parse date onty to microsecunds

tomopongrac's avatar

Maybe you can create helper function with that code so if you in future php will support that you can change only code in that function

Please or to participate in this conversation.