Home > Mobile >  How to convert MySQL timestamp to AWSDateTime with PHP
How to convert MySQL timestamp to AWSDateTime with PHP

Time:12-16

I want to convert MySQL timestamp to AWSDateTime format used in Amplify by PHP.

from
MySQL: 2021-12-16 09:19:05

to
AWSDateTime: 2021-12-16T09:19:05.000Z

CodePudding user response:

With Carbon, which Laravel uses, this will do.

Carbon::parse('2021-12-16 09:19:05', 'UTC')->toIso8601ZuluString("millisecond");

The second parameter of parse method is the timezone that 2021-12-16 09:19:05 is expressed in.

CodePudding user response:

For example you can split value to data & time and use spritf:

<?php
// split string to data & time array
$date_time = explode(' ', '2021-12-16 09:19:05');

// format new datetime string
$AWSDateTime = sprintf("%sT%s.000Z", $datetime[0], $datetime[1]);

echo $AWSDateTime;

PHP sprintf online

Another way (using date_format):

$date = date_create('2021-12-16 09:19:05');
echo date_format($date, 'Y-m-d\TH:i:s.000\Z');

PHP date_format

  • Related