Search code examples
phpmysqllaravelphp-carbon

How to change DateTime format to a desired format via Carbon laravel


I'm using laravel 5.5 and Carbon library to write an API.

I store all date and time values in MySQL database as common YYYY-MM-DD HH:MM:SS format.

But on the other hand front-end developer ask me to return all dates as 2017-12-20T20:30:00.000Z. seems that is a common format in JavaScript.

Is there any way to convert all DateTime formatted fields to desired format via laravel or Carbon ?

Update:
First problem is solved but another one is that client want to send date times as same 2017-12-20T20:30:00.000Z for all fields of table. while I should get and save them as common DateTime. What can I do in this case?


Solution

  • Javascript uses ISO 8601 syntax. In PHP, you can use like this:

    date('c', strtotime($yourDateTime));   // c is ISO 8601 format
    

    Reference here: