I'm using core JSON api to POST/PATCH data from an Android App to my Drupal 8 site. When I need to fill a datetime field I use the next JSON, for example, 2020-02-20T13:15:00. But with this format, this time is UTC, but I need Europe/Madrid timezone. How should I do it?
1 Answer
In DateTimeNormalizer
, the inferface format is RFC3339, which is Y-m-d\TH:i:sP
as an input format string. It also accepts the ISO8601 PHP string, which is Y-m-d\TH:i:sO
Try using strings like in your request '2020-02-20T13:15:00+01:00'.
What I suspect is happening, is that since you aren't specifying the UTC offset, it is being interpreted as '+00:00'
The denormalizer also allepts a format string with the request to parse the value
. I believe it is called format
See also DateTimeNormalizerTest
and EntityTestDatetimeTest
for some more examples.
-
-
I have a question. In Spain we have two timezone. In winter +1h but in summer +2h (we have to add an hour the next March, 29). Then, should I post +2:00 in summer and +01:00 in winter?– briastCommented Feb 21, 2020 at 7:31
-
@briast The proper way is to generate a date/time object on the host that is doing the post and then render it out in the proper format, which will then append the proper offset string. In PHP it would be something like
$value = $date->format('c')
or$value = $date->format('Y-m-d\TH:i:sP')
.– mpdonadio ♦Commented Feb 21, 2020 at 13:38 -