0

I can't figure out how to convert the date from this format: Tue Feb 06 2018 19:41:58 GMT-0500 (Eastern Standard Time) into a format that would be accepted by MySQL DATE type.

I can convert the date with this approach in JavaScript:

var day = Date.getDate();
var month = Date.getMonth() +1;
var year = Date.getFullYear();
var DateInMySqlFormat = year + '-' + '0' + month + '-' + day;

This will work fine. But is there any way that I can convert the date Tue Feb 06 2018 19:41:58 GMT-0500 (Eastern Standard Time) on the server side with PHP? Any help would be greatly appreciated!

Luke
  • 407
  • 2
  • 10
  • 22

2 Answers2

0
$date = 'Tue Feb 06 2018 19:41:58 GMT-0500';
$timestamp = strtotime($date);
echo date('Y-m-d H:i:s', $timestamp);
bumperbox
  • 10,166
  • 6
  • 43
  • 66
0

You can send the DateInMySqlFormat to the backend and then you can create a DateTime object from createFromFormat method, which then can be manipulated with format method to change the format to some other. Available formats from official docs here.

$date = DateTime::createFromFormat('Y-m-d', '2018-02-11');
echo $date->format('Y/m/d'); //2018/02/11
echo $date->format('Y-M-d'); //2018-Jan-11
Nikola Gavric
  • 3,507
  • 1
  • 8
  • 16