I have a date in this format "Wed, 26 Oct 2022 16:11:30 -1100", need to convert it to UTC time and in a format so it can be inserted into a MySQL database.
The date was pulled from an email using "$headerInfo->date;". I don't see any way to receive the date any different.
Every way I try to do the conversion is painful and brute force.
Is there an elegant, or not painful way to do this?
TIA.
Been trying regex but it doesn't handle converting the month into digits, then you have the UTC offset (time zone) to work with.
CodePudding user response:
Parse the date/time string using DateTimeImmutable::createFromFormat, set the timezone to UTC then format it to MySQL's datetime literal syntax
$dt = DateTimeImmutable::createFromFormat('D, j M Y H:i:s O', $dateString);
$mysqlFormat = $dt->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d H:i:sP');
Demo ~ https://3v4l.org/G7RBG
CodePudding user response:
<?php
//just use strtotime
echo date('Y-m-d H:i:s e',strtotime('Wed, 26 Oct 2022 16:11:30 -1100'));
?>