That 'special' date format is ISO8601 probably the most widely used format since is the default for XML
You can, and should, parse this with Carbon and pass the carbon object to your model
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
In my Laravel-5.8 project, I am having this model: Employee
Employee
class Employee extends Model
{
protected $table = 'employees';
protected $primaryKey = 'id';
protected $fillable = [
'staff_code',
'first_name',
'last_name',
'date_of_birth',
];
}
That is,:
App\Employee
Then, in my server I have this table:
CREATE TABLE `employees` (
`id` int NOT NULL auto_increment,
`staff_code` varchar(255) UNIQUE NOT NULL,
`first_name` varchar(255) UNIQUE NOT NULL,
`last_name` varchar(255) NOT NULL,
`date_of_birth` date NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Also I have an external api that comes in form of JSON get request.
https://api.employees.net/allemployees
I have viewed it with postman get request and I have something like this:
{
"ID": "1",
"StaffCode": "STC001",
"FirstName": "Japheth",
"LastName": "Shalom",
"DateOfBirth": "1992-07-11T00:00:00",
},
{
"ID": "2",
"StaffCode": "STC002",
"FirstName": "Ahitophel",
"last_name": "Nedum",
"DateOfBirth": "1991-10-23T00:00:00",
},
{
"ID": "3",
"StaffCode": "STC003",
"FirstName": "Joash",
"FirstName": "Nathan",
"DateOfBirth": "1979-09-22T00:00:00",
},
and so on... this continues
Already I have created this function:
use App\Employee;
public function index()
{
$client = new GuzzleHttp\Client();
$res = $client->request('GET','https://api.employees.net/allemployees');
$clientdatas = json_decode($res->getBody()->getContents(), true);
foreach($clientdatas as $clientdata)
{
$employee = Employee::firstOrNew(['id' => $clientdata['ID']]);
$employee->staff_code = $clientdata['StaffCode'];
$employee->first_name = $clientdata['FirstName'];
$employee->last_name = $clientdata['LastName'];
$employee->date_of_birth = $clientdata['DateOfBirth'];
$employee->save();
}
}
The DateOfBirth that is coming from the external API is carrying a special character (T) along with it. And the data type for date_of_birth in the database is date.
I want to save the data from the external API into the local database.
How do I format DateOfBirth from the API into date data type and remove T00:00:00 from it?
Thank you
Thank you
$employee->date_of_birth = \Carbon\Carbon::parse($clientdata['DateOfBirth'])->toDateString();
Please or to participate in this conversation.