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

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

报告相同问题?

悬赏问题

  • ¥50 cocos2d-x lua 在mac上接入lua protobuf?
  • ¥15 es 7.17.20版本生成时间戳
  • ¥15 wpf dategrid表头排序图标自定义
  • ¥15 xshell无法连接提示ssh服务器拒绝密码
  • ¥15 AT89C52单片机C语言关于串口通信的位操作
  • ¥20 需要步骤截图(标签-服务器|关键词-map)
  • ¥50 gki vendor hook
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 写一个利用ESP32自带按键和LED控制的录像代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。