以下是我收到的警告:
PHP警告:strtotime():依赖系统的时区设置是不安全的。您必需使用date.timezone设置或date_default_timezone_set()函数。如果您使用了这些方法中的任何一种并且仍然收到此警告,则很可能拼错了时区标识符。我们在第38行的/data1/home/spaceweather/Scripts/casesFiles_InsertIntoDataBase.php中选择了'America / Chicago'作为'CST / -6.0 / no DST'
这是我用于在脚本中设置时区的代码。
我正在尝试消除PHP警告并将其指定为UTC到strtotime()而不是我系统上我的时区的默认值。 (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.
然后我使用以下代码将其存储到数据库中:
//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;
答案 0 :(得分:1)
修改您的php.ini
文件并添加与
date.timezone = America/Chicago
或者,您可以在代码中使用date_default_timezone_set()
函数:
date_default_timezone_set('America/Chicago');
有关有效值的列表,请查看http://php.net/manual/en/timezones.php