duanfei7508 2018-05-29 13:33
浏览 300

在PHP中将UTC转换为CST

I want to convert a UTC time to CST using PHP. After googling I got a function

function date_convert($dt, $tz1, $df1, $tz2, $df2) {
  // create DateTime object
  $d = DateTime::createFromFormat($df1, $dt, new DateTimeZone($tz1));
  // convert timezone
  $d->setTimeZone(new DateTimeZone($tz2));
  // convert dateformat
  return $d->format($df2);
}

echo date_convert('2018-05-29 11:44:00', 'UTC', 'Y-m-d H:i:s', 'CST', 'Y-m-d H:i:s');

The output is

2018-05-29 05:44:00

But when I tried converting 2018-05-29 11:44:00 to UTC using online converter, I got the result as 05/29/2018 6:44 AM, which is 1 hour more than what the function returns.

Can anyone help me to find the correct output? Thanks in advance.

  • 写回答

2条回答 默认 最新

  • douren5490 2018-05-29 13:49
    关注

    I can't explain it, but it only seems to happen with EST/CST, even with changing it to a D.

    $n = new DateTime();  // 'date' => '2018-05-29 09:45:01.000000' America/New_York
    $n->setTimeZone(new DateTimeZone('UTC')); // 'date' => '2018-05-29 13:45:01.000000'
    $n->setTimeZone(new DateTimeZone('EDT')); // 'date' => '2018-05-29 08:45:01.000000'
    

    But if you use a place instead of a timezone, it works well:

    $n->setTimeZone(new DateTimeZone('America/New_York'));  // 'date' => '2018-05-29 09:45:37.000000'
    $n->setTimeZone(new DateTimeZone('America/Chicago'));  // 'date' => '2018-05-29 08:45:37.000000'
    

    I'd suggest using a place such as America/Chicago instead.

    评论

报告相同问题?

悬赏问题

  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架