strtotime(): It is not safe to rely on the system's timezone settings

I needed to calculate the number of working days between two dates using PHP. I used the following code from Calculate business days to calculate the number of working days:

Learn PHP as a master
Learn PHP as a master
1x1 px

function getWorkingDays($startDate, $endDate)
{
    $begin = strtotime($startDate);
    $end   = strtotime($endDate);
    if ($begin > $end) {

        return 0;
    } else {
        $no_days  = 0;
        while ($begin <= $end) {
            $what_day = date("N", $begin);
            if (!in_array($what_day, [6,7]) ) // 6 and 7 are weekend
                $no_days++;
            $begin += 86400; // +1 day
        };

        return $no_days;
    }
}

When I displayed the webpage containing the code in my browser, I saw the following error message, though:

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 the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /Users/jasmith1/Documents/www/SGRS/time_sensitive.php on line 21

One way to resolve the problem is to include the PHP statement date_default_timezone_set('timezone'), where timezone is the relevant time zone, in the file where the strtotime function is used before the function is called. E.g.:

date_default_timezone_set('America/New_York');

You can see a list of the time zones a Linux system recognizes using the timedatectl command - see Setting the time zone on a CentOS 7 system. You can determine the current time zone setting on a Mac OS X/macOS system and list the time zones the system recognizes using the systemsetup command - see Setting and viewing time zone information on an OS X system.

Another alternative to resolve the issue is to modify PHP's configuration file, php.ini, which on a Linux or OS X/macOS system will usually be in the /etc directory. You can use the phpinfo() function to display the location of that file along with a lot of other information regarding PHP's configuration. You can add the following lines to the php.ini file, subsituting the appropriate time zone for your system in the date.timezone line:

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = America/New_York

If the php.ini file doesn't exist, you can create it; on a Mac OS X/macOS system, you can do that from a command line using the vi text editor with the command sudo vi /etc/php.ini. When the vi editor opens hit the "i" key to put the editor in "insert" mode, then paste the lines into the file. Then hit the Esc key and type :wq to quit the editor while writing the new lines to the file. After you've updated the php.ini file, you will need to restart the web server software. E.g., for the Apache web server software you can use sudo apachectl restart.

$ sudo apachectl restart
Password:
$

Related articles:

  1. PHP script displaying wrong time
  2. Setting the time zone on a CentOS 7 system
  3. Setting and viewing time zone information on an OS X system
  4. PHP for Apache on OS X El Capitan