0

how to convert Fri Jul 19 2019 00:00:00 GMT 0800 (Singapore Standard Time) this format to mysql date format using PHP? I tried to use strtotime but none of them working for me

date("Y-m-d H:i:s", strtotime("Fri Jul 19 2019 00:00:00 GMT 0800 (Singapore Standard Time)"));
3
  • The date fails to parse due to Double timezone specification (from the PHP error). Commented Jul 20, 2019 at 8:46
  • Do you control the formatting of the date? Commented Jul 20, 2019 at 8:46
  • I'm not controlling the format of the date Commented Jul 20, 2019 at 8:50

1 Answer 1

1

The date fails to parse because it's in a very weird format.

If you can't control the format of the incoming date you could grab the different parts using regex and parse them:

$rawDate = "Fri Jul 19 2019 00:00:00 GMT 0800 (Singapore Standard Time)";
preg_match('/(.*?) GMT (\d+)\s\(.*?\)/', $rawDate, $matches);

$date = DateTime::createFromFormat('D M j Y H:i:s', $matches[1])
   ->setTimezone(new DateTimeZone('+' . $matches[2]));

echo $date->format('Y-m-d H:i:s'); // outputs 2019-07-19 06:00:00

Example: https://3v4l.org/fbuMk

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.