Converting datestring from request to MySQL compatible date format

Viewed 42

I am getting a date as a string from an Ajax call: "Wed Jun 17 2020 09:26:45 GMT+0200 (Mitteleurop\u00e4ische Sommerzeit)" and tried to format it in "Y-m-d H:i:s" but failed. I tried

$my = $request->input('date');
$date = Carbon::parse($my);
$date = date("Y-m-d H:i:s", strtotime($my));
$date = Carbon::createFromFormat('Y-m-d H:i:s', $my);
$date = Carbon::create(my);

but nothing worked. Carbon works and $request->input('date') exists. What is the issue here?

1 Answers

As PHP would not handle every possible timezones in every possible languages, Mitteleurop\u00e4ische Sommerzeit make Carbon::parse but GMT+0200 is enough for the timezone of 1 particular moment, so you can remove the extra timezone:

$my = $request->input('date');
$my = preg_replace('/\s*\(.*\)$/', '', $my);
$date = Carbon::parse($my);
$date = date("Y-m-d H:i:s", strtotime($my));
$date = Carbon::createFromFormat('Y-m-d H:i:s', $my);
$date = Carbon::create(my);

Still you should consider this is a work-around. The correct approach is to send dates with a ISO string (in GMT timezone), and a clean timezone such as Europe/Berlin in an other field if the timezone matters.

Related