如何在Laravel中使用PHP将ISO 8601“ 20200823T203851.000Z”转换为可读的日期和时间格式?

问题描述

我想使用Laravel控制器中的PHP"20200823T203851.000Z"日期格式转换为可读格式。我尝试了date(),结果返回了"20200823UTC203851.0000"strtotime()似乎不起作用。

 public function index() {

    $allCards = $this->getCardInfo();
    $clanInfo = $this->getClanInfo();
    $dateTime = $clanInfo->memberList[0]->lastSeen;

    //data stored on dateTime = '20200823T203851.000Z'
    //required in readable format

    return view( 'claninfo',compact( ['allCards','clanInfo'] ) );
}

解决方法

您可以使用Carbon进行操作,请从here签出。

现在让我们$dateTime可读:

 public function index() {

    $allCards = $this->getCardInfo();
    $clanInfo = $this->getClanInfo();
    $dateTime = $clanInfo->memberList[0]->lastSeen;

    //data stored on dateTime = '20200823T203851.000Z'
    //Required in readable format

    // here is the change:
    $dateTime = \Carbon\Carbon::parse($dateTime)->format('g:i a l jS F Y')
    // now $dateTime will be something like this: 7:30 pm Monday 24th August 2020

    return view( 'claninfo',compact( ['allCards','clanInfo'] ) );
}