【问题标题】:cakephp 3 - passing dates with jsoncakephp 3 - 使用 json 传递日期
【发布时间】:2016-05-17 16:38:07
【问题描述】:

Cakephp3,我正在使用 Json 将数据传递到另一个页面,我的问题似乎出在我的新页面上,我创建的日期作为字符串传递,并显示为“2016-05-16T17:21:10+0000” .如何格式化该字符串以留下 '2016-05-16' ?我尝试将其设置为日期,我尝试使用 i18nFormat 没有成功。

$http = new Client();
$response = $http->get('http://localhost:8383/mb-be/apis/getmaritimemessagelist.json');
$maritimes = $response->json['content'];
$cnt = count($maritimes);

for ($i=0; $i<$cnt; $i++) {    
    //var_dump($maritimes[$i]);
    echo '<p class="brdr-bttm mrgn-lft-md ">';
    echo  $this->Html->link($maritimes[$i]['title'], array(
            'controller' => 'messages',
            'action' => 'view/'. $maritimes[$i]['id']
        ));
     echo '<br />';

     $date = $maritimes[$i]['created'];
     echo $date; 
     echo '</p>'; 
}

【问题讨论】:

    标签: json cakephp cakephp-3.0


    【解决方案1】:

    我找到了这个问题的解决方案。 在这里您可以使用DateTime::createFromFormat 来格式化您的created date。但是日期'2016-05-16T17:21:10+0000' 内的T 仍然存在问题,只是因为时区(我猜)。

    您可以将此T 替换为space 并根据需要格式化created date

    $http = new Client();
    $response = $http->get('http://localhost:8383/mb-be/apis/getmaritimemessagelist.json');
    $maritimes = $response->json['content'];
    $cnt = count($maritimes);
    
    for ($i=0; $i<$cnt; $i++) {    
        //var_dump($maritimes[$i]);
        echo '<p class="brdr-bttm mrgn-lft-md ">';
        echo  $this->Html->link($maritimes[$i]['title'], array(
                'controller' => 'messages',
                'action' => 'view/'. $maritimes[$i]['id']
            ));
         echo '<br />';
    
         $date = $maritimes[$i]['created'];
    
            //--------- Format created date --------
        $string =  str_replace("T"," ",$date);
    
        $created_date = DateTime::createFromFormat('Y-m-d H:i:sO',$string)->format('Y-m-d');
    
        echo $created_date;
         echo '</p>'; 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-05
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多