I am connecting to an API and its returning the date in UnixDate, not unix timestamp. How do I convert this format to be able to save it in a datetime field?
Example: 'Mon Jun 09 21:59:59 UTC 2025'
you can simply do
$date = date('Y-m-d H:i:s', strtotime('Mon Jun 09 21:59:59 UTC 2025'));
echo $date;
The PHP date() command will turn a UNIX timestamp into a multitude of different formats. So, abstractly, if you do the following:
date(*formatCodeString*, *timeStampInt*);
You should be able to get the format that you need.
The MySQL 5.7 Manual States.
The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
Thus we need to create a format code string that represents the form YYYY-MM-DD HH:MM:SS
.
From the PHP manual for date()
.
YYYY = Y
MM = m
DD = d
HH = H
MM = i
SS = s
PHP date()
format string for MySQL DATETIME
=
'Y-m-d H:i:s'
Use the PHP function stringtotime(). The PHP manual states that this:
... parse[s] about any English textual datetime description into a Unix timestamp.
UNIX timestamp =
strtotime('Mon Jun 09 21:59:59 UTC 2025');
date()
MySQL DATETIME
string =
date('Y-m-d H:i:s', strtotime('Mon Jun 09 21:59:59 UTC 2025'));
That should do it. Good luck!