douxitao8170 2014-01-16 17:04
浏览 53
已采纳

strtotime()时区警告。 使用UTC时区在MySQL数据库中存储简单的Unix时间戳[重复]

This question already has an answer here:

Here is the warning I get:

PHP Warning: strtotime(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CST/-6.0/no DST' instead in /data1/home/spaceweather/Scripts/casesFiles_InsertIntoDataBase.php on line 38

Here is my code for what I'm using to set my timezone in my script.

I'm trying to eliminate the PHP Warning and specify it as UTC to strtotime() NOT my default to my time zone on my system. (P.S. Go Blackhawks!)

$year = substr($filePath[$row], -17,4);  //where this outputs a simple year 'CCYY'
$day = $days2[$days2keys[$i]];           //where this provides the day of year
$format = 'Y-z';                          //specifying what format i'm creating the datetime with
$date = $year . '-' . $day;             //formatting the strings to the above $format
$timezone = new DateTimeZone('UTC');     //specify the timezone
$fileDateStore[$row] = DateTime::createFromFormat($format, $date, $timezone);  //create the DateTime object
$fileDateString[$row] = date_format($fileDateStore[$row],"Y-m-d".PHP_EOL);  //format it so strtotime() can read it
$fileDate[$row] = strtotime($fileDateString[$row]);  //finally create the Unix Timestamp for the date.

And then later on I store this into the database using the following code:

//connect to the database
$con=mysqli_connect("server","database","username","password");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
for($j = 1; $j < $numrows; $j++) {
    $date = $fileDate[$j]; 
    mysqli_query($con,"INSERT INTO tablename (fileDate) 
    VALUES (".$date.")");
}
mysqli_close($con);
echo "task finished and database connection closed.".PHP_EOL;
</div>
  • 写回答

1条回答 默认 最新

  • douxugu5836 2014-01-16 17:19
    关注

    Edit your php.inifile and add something similar to

    date.timezone = America/Chicago
    

    Alternatively, you can use the date_default_timezone_set() function in your code:

    date_default_timezone_set('America/Chicago');
    

    For a list of valid values, check out http://php.net/manual/en/timezones.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧