Home > OS >  PHP unable to format Datetime to correct string format
PHP unable to format Datetime to correct string format

Time:12-13

I have this format:

How do I use PHP DateTime to format like 2023-01-27T17:37:00.000Z

So far I have

$date = new DateTime();
$string = $date->format('Y-m-dTHH:H:i:s');

but it outputs 2022-11-25UTC0000:00:00:00

What is the correct format

Is there a ressource on the web that would find it for me ? like a helper website.

CodePudding user response:

You can do as follows:

$date = new DateTime();
$string = $date->format('Y-m-d\TH:i:s.000\Z');
echo $string;

CodePudding user response:

You can use the DateTime class to format the date like this:

$date = new DateTime('2023-01-27T17:37:00.000Z');
echo $date->format('Y-m-d\TH:i:s.u\Z'); // Outputs 2023-01-27T17:37:00.000Z
  • Related