Home > Software design >  How do you format date as the following example: 2021-08-17T10:19:02.019Z in PHP
How do you format date as the following example: 2021-08-17T10:19:02.019Z in PHP

Time:02-21

I have use PHP to convert a simple datetime from MySQL

example: 2022-02-21 10:26:27

to a format like this one:

2021-08-17T10:19:02.019Z.

What function and formatting pattern should I use?

Also, could you please help me understand what the .019Z stand for?

Thank you!

CodePudding user response:

The format you're looking for is ISO8601.

You can use the defined constants in DateTime object in PHP:

echo $objDateTime->format('c');
echo $objDateTime->format(DateTime::ISO8601);

See also DateTime formats in PHP documentation and the DateTime class itself

CodePudding user response:

DateTime::format -- DateTimeImmutable::format -- DateTimeInterface::format -- date_format — Returns date formatted according to given format

https://www.php.net/manual/en/datetime.format.php

  • Related