-1

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;
Dr. Dan
  • 205
  • 2
  • 11
  • What's your question? I can't see you've done anything of what the message says. – Álvaro González Jan 16 '14 at 17:08
  • See Edits above. Sorry! – Dr. Dan Jan 16 '14 at 17:11
  • **You are required to use the date.timezone setting or the date_default_timezone_set() function**... and you edit to add your SQL code?? – Álvaro González Jan 16 '14 at 17:17
  • Just trying to make the post more complete. Sorry. Adding the SQL at the end seemed like it might be helpful and was an afterthought XD. – Dr. Dan Jan 16 '14 at 17:20
  • Sorry Alvaro i guess my question was confusing. If you notice in my code i put the line `$timezone = new DateTimeZone('UTC');` I'd like it to go to that timezone not my default. very sorry for the confusion and thanks for your help. – Dr. Dan Jan 16 '14 at 17:25

1 Answers1

1

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

Sébastien
  • 2,236
  • 2
  • 20
  • 28
  • Thank for your help user2534880. I made my question confusing. I want to set the timezone to 'UTC' by this line of code `$timezone = new DateTimeZone('UTC');` but i'm still getting the error for some reason. I think this answer would be how to change my default timezone for all php related functions? – Dr. Dan Jan 16 '14 at 17:26
  • You get the error because you're ignoring the instructions it provides. You even ignore those instructions when someone helpfully copies them into an answer. Do you have any suggestion on how we can convince you to change `date.timezone` or call `date_default_timezone_set()`? – Álvaro González Jan 16 '14 at 17:29
  • Setting the default timezone will not prevent you to specify a different DateTimeZone (such as UTC) on the DateTime object you're creating. But it will make the warning go away. – Sébastien Jan 16 '14 at 17:35
  • Okay, so i guess i was confused on the behaviour of date_default_timezone_set(). It looked like a function that sets the timezone throughout the whole script. What if I'm someone who stores dates with multiple timezones or different timezones? If i put this date_default_timezone_set() function at the top of a script how can I later change that time zone? Can i place that line in multiple points in the script. Sorry i'm new to php. – Dr. Dan Jan 16 '14 at 17:40
  • Okay, I understand now the error stems from the fact that php does not have a default even set and that it is relying on my system. One issue I am having now is that I need to contact my host admin to change it as the php.ini file is located outside of the directory i have permissions in :(. – Dr. Dan Jan 16 '14 at 17:55
  • If you don't have access to the `php.ini` file, you can still use the `date_default_timezone_set()` function in your code. – Sébastien Jan 16 '14 at 18:38
  • @ZealotSveta - In fact, your error is being triggered by `strtotime()`, where you do use the system default time zone. And you don't need to contact your host admin at all to do what the error message says. – Álvaro González Jan 20 '14 at 17:02