【问题标题】:Date function result wrong time in CodeIgniter and date_default_timezone_set('Asia/Singapore'); in Config file not working日期函数导致 CodeIgniter 和 date_default_timezone_set('Asia/Singapore') 中的时间错误;在配置文件中不起作用
【发布时间】:2017-03-01 03:28:38
【问题描述】:

就像标题一样,我对日期(Y-m-d H:i:s)有疑问;给出错误时间的功能。我已经更改了 config.php 文件,添加了 date_default_timezone_set('Asia/Singapore');在文件的顶部,它没有完全工作。

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
date_default_timezone_set('Asia/Singapore');

但是,如果我将它添加到使用日期函数作为示例的方法/函数中,它会起作用:

function giveMyDate(){ 
date_default_timezone_set('Asia/Singapore'); 
echo(date('Y-m-d H:i:s')); 
}

谁能解释为什么我在 config.php 中添加 date_default_timezone_set 不起作用?我必须将其添加为全局原因,因为使用日期函数的代码过多。

上面的情况与此处类似,但我已经尝试过该建议但不起作用: CodeIgniter date - wrong time

【问题讨论】:

  • echo(date(Y-m-d H:i:s)); 这实际上是您的代码吗?还是echo(date('Y-m-d H:i:s'));
  • 对不起,我已经更新了代码@augwa
  • 您能否更新您的代码以提供您所期望的与您得到的?
  • 我添加了与我的案例类似的其他参考资料,您可以阅读它,谢谢@augwa
  • 如果您只是添加了您的具体情况,将会很有帮助。

标签: php codeigniter


【解决方案1】:

您是否尝试在php.ini更改 时区

[Date]
date.timezone=Asia/Singapore

更多参考这里http://www.inmotionhosting.com/support/website/php/setting-the-timezone-for-php-in-the-phpini-file

【讨论】:

  • 我无法更改 php.ini,因为我无法访问它(类似于共享主机)。 @keith-asilom
  • 抱歉不知道您可以访问 php.ini,也许您可​​以尝试将 ini_set('date.timezone', 'Asia/Singapore') 放在 index.php 中
猜你喜欢
  • 2016-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多