0

I am working with PHP,And trying to convert "dynamic datetime" ( can be with anytimezone) into "GMT Format", How can i do this ? I tried with following code but not working for me

echo gmdate('d.m.Y H:i', strtotime('2023-03-13 11:45 AM'));

How can i convert above "datetime" into "gmt" format ?

Maria
  • 1
  • Use the DateTime class instead. Answer provided over at https://stackoverflow.com/questions/2095703/php-convert-datetime-to-utc – Bellator Mar 13 '23 at 07:27
  • To convert from A to B you need to know what A and B are. In the code you've shared, there's no clue about what the local time is meant to be. – Álvaro González Mar 13 '23 at 08:25

1 Answers1

0

It pretty much works out of the box:

echo gmdate('d.m.Y H:i', strtotime('2023-03-13 11:45 AM America/New_York'));
echo gmdate('d.m.Y H:i', strtotime('2023-03-13 11:45 AM Europe/Dublin'));
echo gmdate('d.m.Y H:i', strtotime('2023-03-13 11:45 AM Asia/Tokyo'));
13.03.2023 15:45
13.03.2023 11:45
13.03.2023 02:45

Demo

But your input does not contain any information about what the local time is meant to be, so PHP will default to whatever local time you've set for PHP.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360