php convert datetime to UTC

后端 未结 16 1103
無奈伤痛
無奈伤痛 2020-11-27 12:30

I am in need of an easy way to convert a date time stamp to UTC (from whatever timezone the server is in) HOPEFULLY without using any libraries.

16条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 12:47

    If you have a date in this format YYYY-MM-HH dd:mm:ss, you can actually trick php by adding a UTC at the end of your "datetime string" and use strtotime to convert it.

    date_default_timezone_set('Europe/Stockholm');
    print date('Y-m-d H:i:s',strtotime("2009-01-01 12:00"." UTC"))."\n";
    print date('Y-m-d H:i:s',strtotime("2009-06-01 12:00"." UTC"))."\n";
    

    This will print this:

    2009-01-01 13:00:00
    2009-06-01 14:00:00
    

    And as you can see it takes care of the daylight savings time problem as well.

    A little strange way to solve it.... :)

提交回复
热议问题