-2

As in title, I need to convert (for example) "Thu, 08 Oct 2020 08:32:44 GMT" format to Y-m-d H:i:s in php.

Anyone know how to do that ?

  • Does this answer your question? [php date format](https://stackoverflow.com/questions/3613482/php-date-format) – JaviL Oct 08 '20 at 11:37

3 Answers3

2

Use DateTime::createFromFormat. It allows to specify the format of the input, and with format you can specify the desired output:

$dateTime = DateTime::createFromFormat('D, d M Y H:i:s e', 'Thu, 08 Oct 2020 08:32:44 GMT');
echo $dateTime->format('Y-m-d H:i:s');  // 2020-10-08 08:32:44

The documentation provides all the formatting characters you can use.

trincot
  • 317,000
  • 35
  • 244
  • 286
1

Make use of strtotime and date function See Demo

$ php -r '$str="Thu, 08 Oct 2020 08:32:44 GMT"; print date("Y-m-d H:i:s",strtotime("$str"));'
2020-10-08 08:32:44

Better readable version

<?php

$str="Thu, 08 Oct 2020 08:32:44 GMT"; 
print date("Y-m-d H:i:s",strtotime("$str"));

?>
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
  • You're welcome, above output in php fiddle is from timezone US/Pacific, I hope your timezone set [with timezone](http://sandbox.onlinephpfunctions.com/code/7cc39ae53c18890d4f46329b36042b110942f0c6) – Akshay Hegde Oct 08 '20 at 11:19
  • Akshay Hegde, maybe You know how to convert date format (like You wrote) but in foreach loop ? – Paweł Smacki Oct 08 '20 at 17:29
  • @PawełSmacki just pass value over iteration in place of `$str` – Akshay Hegde Oct 09 '20 at 04:32
1

Just use date to convert that. Pass the format string and the date in Unix timestamp (you can convert it using strtotime).

Example:

$date = "Thu, 08 Oct 2020 08:32:44 GMT";
$newDate = date("Y-m-d H:i:s", strtotime($date));
Vladoski
  • 317
  • 2
  • 9