strtotime() time zone warning. Storing a simple Unix Timestamp in a MySQL database with UTC Timezone [duplicate]

StackOverflow https://stackoverflow.com/questions/21168181

Question

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;
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top