-1

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!

EDIT:

Using echo date("c"); results in an output like this one: 2022-02-21T14:14:22+01:00, which differs form the desired output in the latest part (milliseconds and timezone).

On the other hand, if I do echo date("Y-m-d\TH:i:s.v"); I get an output like the following: 2022-02-21T14:22:24.000, where I should only be missing the timezone. I am not able to find it in the documentation.

The server isn't on the standard timezone, and I would like not to change it. Is there a way to get it dynamically?

StackAle
  • 37
  • 10

2 Answers2

1

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

Frederik Spang
  • 3,379
  • 1
  • 25
  • 43
  • I'm used to use the date() function, using date("c"); shoul be equivalent to $objDateTime->format('c'). But I get this output: 022-02-21T14:22:24+01:00, which differs in the latest parte, about milliseconds and timezone. – StackAle Feb 21 '22 at 13:24
-2

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

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

Lol Master
  • 33
  • 7