我搜索一种方式来格式化从json响应给出的日期。目前我可以通过经由JSON响应的时间到两个格式:格式化从json给出的日期
字符串格式:
{"date":"2016-11-25T11:24:54+0100"}
或对象格式:
"date":{"timezone":{"name":"Europe\/Paris","location":{"country_code":"FR","latitude":48.86666,"longitude":2.33333,"comments":""}},"offset":3600,"timestamp":1480069808}}
我必须格式化的这种两种JSON格式之一进入该格式: dd/mm/aaaa
,示例:2016年11月25日
此操作必须在我的视图中完成,使用jQuery/js或控制器R之前,发送JSON的回复:
/* $normalizer = new GetSetMethodNormalizer(); here the code for fomat the date into string
$callback = function ($dateTime) {
return $dateTime instanceof \DateTime
? $dateTime->format(\DateTime::ISO8601)
: '';
};
$normalizer->setCallbacks(array('date' => $callback));*/
$normalizer->setIgnoredAttributes(array('article'));
$serializer = new Serializer(array($normalizer), array($encoder));
$dataJSON = $serializer->serialize($data, 'json');
$response = new Response();
$response->setContent($dataJSON);
$response->headers->set('Content-Type', 'application/json');
return $response;