【发布时间】:2026-02-09 00:05:02
【问题描述】:
这是我收到的警告:
PHP 警告:strtotime():依赖系统的时区设置是不安全的。您需要使用 date.timezone 设置或 date_default_timezone_set() 函数。如果您使用了这些方法中的任何一种,但仍然收到此警告,您很可能拼错了时区标识符。我们在第 38 行的 /data1/home/spaceweather/Scripts/casesFiles_InsertIntoDataBase.php 中为“CST/-6.0/no DST”选择了“America/Chicago”
这是我用来在脚本中设置时区的代码。
我正在尝试消除 PHP 警告并将其指定为 UTC 到 strtotime() 而不是我系统上的默认时区。 (附:黑鹰队!)
$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;
【问题讨论】:
-
你有什么问题?我看不出你已经完成了消息中所说的任何事情。
-
参见上面的编辑。对不起!
-
您需要使用 date.timezone 设置或 date_default_timezone_set() 函数...并且您编辑以添加您的 SQL 代码??
-
只是想让帖子更完整。对不起。在最后添加 SQL 似乎可能会有所帮助,并且是事后才想到的 XD。
-
对不起,阿尔瓦罗,我想我的问题令人困惑。如果您在我的代码中注意到我输入了
$timezone = new DateTimeZone('UTC');行,我希望它转到那个时区,而不是我的默认时区。非常抱歉给您带来的困惑,感谢您的帮助。
标签: php mysql datetime timezone strtotime