PHP - 提取文件的修改时间设置为服务器的时区 - 如何更改?

时间:2018-05-29 13:46:22

标签: php timezone timestamp zip extract

因此,我们的代码会创建一个zip文件,其中包含来自特定文件夹的PDF文件。一切正常,但将此zip文件下载到计算机并提取文件时 - 每个PDF文件的修改时间都遵循服务器的时区。 (我们的网站位于GoDaddy共享服务器上,他们位于亚利桑那州。我们位于新加坡。)

有没有办法修改这个?我试图设置默认时区或使用touch()但没有运气。

// Initialize archive object
$zip = new ZipArchive();
$res = $zip->open(plugin_dir_path( __FILE__ ).'MortgageWise-All-Reports.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);

if ($res === TRUE) {
 // Create recursive directory iterator
 /** @var SplFileInfo[] $files */
 $files = new RecursiveIteratorIterator(
  new RecursiveDirectoryIterator($rootPath)
 );

 foreach ($files as $name => $file) {
 // Skip directories (they would be added automatically)
  if (!$file->isDir()) {
   // Get real and relative path for current file
   $filePath = $file->getRealPath();
   $relativePath = substr($filePath, strlen($rootPath));

   date_default_timezone_set("Asia/Singapore");

   //touch($filePath, );

   // Add current file to archive
   $zip->addFile($filePath, $relativePath);
  }
 }

 // Zip archive will be created only after closing object
 $zip->close();
}
else {
 echo "File not created.";
}

1 个答案:

答案 0 :(得分:0)

我首先尝试在创建ZIP之前在脚本开头设置时区,而不是在foreach循环中。

但话虽如此,我不相信它会产生很大的不同,因为PDF文件已经存在?因此,已经分配了这些属性!

您是否可以访问Web Host Manager或类似设备?如果您有VPS或Dedicated服务器,则可以自行更改服务器的时区。

编辑:抱歉,您已经声明自己在共享服务器上,强烈建议使用VPS让您完全控制这些设置,每月只需50美元