即使设置了date_default_timezone_set,PHPExcel也会获得错误的时区 [英] PHPExcel gets wrong timezone even after setting date_default_timezone_set

查看:257
本文介绍了即使设置了date_default_timezone_set,PHPExcel也会获得错误的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的一个项目中使用 http://phpexcel.codeplex.com ,并且我有出了问题. 我想在一个单元格中写入time()值:

I'm using http://phpexcel.codeplex.com in one of my project, and I have come into an issue. I want to write the time() value inside a cell, which I am doing with :

function writeTimeLine($objActiveSheet, &$lineNumber, $timeStart, $timeEnd, $duration, $category, $client, $date, $comment)
{
    $objActiveSheet->setCellValue('A'.$lineNumber, PHPExcel_Shared_Date::PHPToExcel( $timeStart ));
    $objActiveSheet->getStyle('A'.$lineNumber)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4);
}

$ objActiveSheet引用$ objPHPExcel-> getActiveSheet()

The $objActiveSheet refers to $objPHPExcel->getActiveSheet()

Excel中的结果是:

And the result in Excel is:

20:27:39

当我在计算机上进行实际测试时,它是16:27:39

When in real on the computer I'm testing it it's 16:27:39

所以这是时区的问题(我居住在美国东部--4). 但是,我将默认时区设置为

So it's an issue with the timezone (I'm living in Eastern America which is -4). However, I'm including PHPExcel files after setting the default timezone with

date_default_timezone_set('America/New_York');

即使回显了time(),我也确实看到了正确的小时(16:27:39).

And even with an echo of the time() I do see the correct hour (16:27:39).

这是PHPExcel的错误还是在这里做错了?

Is it a bug of PHPExcel or am I doing something wrong here?

感谢您的帮助.

推荐答案

刚刚发现了问题:PHPExcel转换为内部的UTC时间: PHPExcel/Shared/Date.php

Just found the problem: PHPExcel translates to UTC time inside: PHPExcel/Shared/Date.php

函数PHPToExcel将时区临时更改为UTC,然后返回默认时区.

The function PHPToExcel change temporarily the timezone to UTC, and then go back to the default timezone.

解决方案很简单,但需要更改该文件. 只需复制粘贴PHPToExcel函数,将其命名为PHPToExcelWithoutUTC(或您想要的任何名称),然后注释以下几行:

The solution is simple but requires to change that file. Simply copy paste the PHPToExcel function, name it PHPToExcelWithoutUTC (or any name you want) and comment the following lines:

//$saveTimeZone = date_default_timezone_get();
//date_default_timezone_set('UTC');

//date_default_timezone_set($saveTimeZone);

它有效.

默认情况下,似乎打算始终使用UTC时间.我不会删除此线程,因为它对于那些想要用户时区的人可能有用.

It seems it's intended to always use UTC time by default. I'm not deleting this thread because it might be useful for those who want the user timezone.

这篇关于即使设置了date_default_timezone_set,PHPExcel也会获得错误的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆