简体   繁体   English

从Unix时间戳到PHP年月日的转换

[英]Unix Timestamp to PHP Year Month and Day Conversion Off

We are in the process of moving our domain to a new dedicated server we recently purchased. 我们正在将域迁移到我们最近购买的新专用服务器上。 The new server's time zone is causing some issues with our attachments php code. 新服务器的时区导致附件php代码出现一些问题。

Our forum software uses the following folder structure to store attachments: 我们的论坛软件使用以下文件夹结构来存储附件:

/public_html/forum/files/2016/February/14/[Filename] / public_html / forum / files / 2016 / Febuary / 14 / [文件名]

The year, month and day are obtained from the File Upload Time Stamp which is a Unix Timestamp such as "1455426488". 年,月和日可从文件上载时间戳获取,该时间戳是Unix时间戳,例如“ 1455426488”。 This timestamp is then converted to year, month and day using the following php code: 然后使用以下php代码将此时间戳转换为年,月和日:

$date = getdate((int)$attachment['filetime']);
$filepath = $config['upload_path'] . '/' . (string)$date['year'] . '/' . $date['month'] . '/' . (string)$date['mday'];

This worked fine on our old server and the server before it, but on the new server the "day" is either 1 day behind or one day ahead when converted, which causes "February 14" file uploads to either end up in "February 13" folder or "February 15". 这在我们的旧服务器和之前的服务器上都可以正常工作,但是在新服务器上,转换后的“天”要么落后1天,要么提前一天,这导致“ 2月14日”文件上传到“ 2月13日”结束”文件夹或“ 2月15日”。 Keep in mind that file Upload time of 1455426488 in unix timestamp is 02/14/2016 at 5:08am UTC Timezone. 请记住,Unix时间戳中的1455426488的文件上传时间为2016年2月14日UTC时区。

Does anyone have any suggestions on how to fix this issue? 有人对如何解决此问题有任何建议吗? This is a critical part of our forum system, so without coming up with a solution for it we won't be able to migrate to the new server. 这是我们论坛系统的关键部分,因此,如果没有解决方案,我们将无法迁移到新服务器。

Thanks, 谢谢,

Pete 皮特

BTW, both the old and the new server are using the "America/Chicago" as their timezone (CST). 顺便说一句,新旧服务器都使用“美国/芝加哥”作为其时区(CST)。

You can use the DateTime class in order to convert your dates. 您可以使用DateTime类来转换日期。

$date = new DateTime(null, new DateTimeZone('America/Chicago'));
$date->setTimeStamp((int)$attachment['filetime']);
$filepath = $config['upload_path'] . '/' . $date->format('Y') . '/' . $date->format('F') . '/' . $date->format('d');

I found the problem! 我发现了问题! It looks like the following line had found it's way into the download/file.php file: 似乎以下行已找到它进入download / file.php文件的方式:

date_default_timezone_set('GMT');

This line did not exist in the original file, so removing it resolved the issue! 该行在原始文件中不存在,因此删除它可以解决问题! I am still not sure how this line got added, but it explains the weird behavior. 我仍然不确定这行是如何添加的,但是它解释了奇怪的行为。

Thanks, Pete 谢谢,皮特

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM