2016-11-25 62 views
0

我搜索一种方式来格式化从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; 

回答

1

使用PHP的建设DateTime类像

<?php 

$date = new Datetime('2016-11-25T11:24:54+0100'); 
var_dump($date->format('d.m.Y h:i:s')); // string(19) "25.11.2016 11:24:54" 
1

您的控制器独立如何发送数据,在您的视图(演示文稿),您可以通过Moment.js格式化。

例如:

// Your controller date timestamp 
const timestamp = 1480070762; 
moment().unix(timestamp).format('DD-MM-YYYY'); 

我建议你你的控制器充当API和所有的意见有flexability的选择日期的显示格式。