Php datetime convert to ISO 8601

66
April 28, 2021, at 03:20 AM

I have an app that would call an Amazon api, the datetime requirements should be an ISO 8601 format. I tried the Carbon like

   'data_startTime' => 'nullable|date_format:Y-m-d H:i:s',
   Carbon::parse($request->data_startTime)->toIso8601String()

The example data_startTime

  2019-12-10 20:11:24

Which resulted to

   2019-12-10T20:11:24+00:00

But per Amazon's requirement, it will just reject that param and will throw error What Amazon required is the format should like this

    2019-12-10T20:11:24.000Z

How to achieve that using Carbon or any Php DateTime class ?

Answer 1

One solution is to use the DateTime class, select the time zone "Z" and generate the desired format with the method of the same name.

$start = '2019-12-10 20:11:24';
$strDate = date_create($start,new DateTimeZone('Z'))->format('Y-m-d\TH:i:s.ve');
echo $strDate;  //2019-12-10T20:11:24.000Z
Answer 2

If microsecond-precision is OK, toJSON might be the cleanest option:

echo Carbon::parse('2019-12-10 20:11:24')->toJSON();

Note that if you already JSON encode the body you send, it will just be handled automatically:

echo json_encode([
  'date' => Carbon::parse('2019-12-10 20:11:24')
]);
READ ALSO
Implementing a dual sidebar angular

Implementing a dual sidebar angular

I am trying to implement a sidebar within a sidebar for an angular projectBasically when I click on projects, a new sidebar should replace the current sidebar with a completely different set of options

34
Reading second row from excel using apache poi

Reading second row from excel using apache poi

I am trying to read the first and second row of the excel , I am not able to figure out the way to read the second row of excel and compare the values at the first execution

108
Python - Menu with live data

Python - Menu with live data

I need help with my menuI created menu like this, but my menu must be have live data

128
Can't popup modal in the fullcalendar

Can't popup modal in the fullcalendar

I have a problem creating a popup modal in the full calendar functionI using this code to let the modal box popup $('#createEventModal')

42