0

I want to convert "20200823T203851.000Z" this date format into a readable format using PHP in a Laravel controller. I tried date() and it returns "20200823UTC203851.0000" as result, strtotime() does not seem to work.

 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'] ) );
}
Joundill
  • 6,828
  • 12
  • 36
  • 50

1 Answers1

3

You can use Carbon to do so, please checkout it out from here

now let's make the $dateTime readable:

 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'] ) );
}
Mahmood Ahmad
  • 452
  • 4
  • 11
  • Carbon\Exceptions\InvalidFormatException Could not parse '20200823T203851.000Z': DateTime::__construct(): Failed to parse time string (20200823T203851.000Z) at position 16 (0): Unexpected character , I am getting this error for digits after decimal I guess that is millisecond or idk. How do I fix it ? – Abhisan Ghimire Aug 24 '20 at 09:34
  • please checkout https://stackoverflow.com/questions/4411340/php-datetimecreatefromformat-doesnt-parse-iso-8601-date-time and https://stackoverflow.com/questions/52505498/carbon-parse-iso-8601-string-to-utc-date-and-record-it-to-db and https://stackoverflow.com/questions/52525047/carbon-parse-to-iso8601 ... if you still have a problem please send again a comment to inform me – Mahmood Ahmad Aug 24 '20 at 21:02