1

I am using the lighttpd webserver on a pi running raspbian stretch, with php version 7.0.

I can get a string representing the current date, time and timezone information onto a web page by calling the PHP function date('l jS \of F Y h:i:s a e').

However, if I change the timezone information in localisation options using raspi-config (version 20180518), then the timezone changes aren't reflected in the page served by the web server until I reboot the pi.

I believe that the changes caused by the the timezone changes are limited to rewriting the contents of the /etc/timezone file and creating a link from /etc/localtime to a relevant file under /usr/share/zoneinfo. Please can someone correct me if raspi-config is doing more?

I guess that lighttpd and PHP are somehow caching the timezone information and not recognising any changes. Can I force PHP to fetch the timezone information from the OS each time date() is called in some way?

By the way, this problem is NOT to do with web browser caching - reloading the web page shows a different date and time (but not timezone).

1
  • This one line of code should be shown to anyone considering to learn PHP. Feb 9, 2021 at 12:24

1 Answer 1

1

PHP has timezone defined in php.ini as date.timezone = .... When this value is empty, PHP take timezone from system as you want. But, only once. It take place when php.ini is parsed at start or restart of PHP. So, when you change system timezone you must also restart PHP.

3
  • Thanks for the suggestion of a work-around Luckasz, but I am really loooking for a way to get PHP to fetch the timezone each time automatically. The phpinfo() function gives lots of info about the inner workings of PHP including a section on the operation of the cache. I thought that perhaps tweaking a value here might give me what I want. Furthermore, I was trying to get the timezone to be settable via a web page, using the back-end cron service. If someone is trying to set the timezone through a web page using PHP, then restarting PHP might cause problems. Any further ideas? Jun 13, 2018 at 20:51
  • It will be tricky, because as far as I know, PHP sets timezone only under startup of service. But you can do the trick: (1) Change timezone, (2) write to some file flag that tz were changed, (3) In script under cron periodically check the flag. if lfag is set reload PHP and clear flag. Use systemctl reload ... to gracefully reload service configs without interrupting webpages. Jun 14, 2018 at 6:36
  • That trick does seem to work. I used systemctl restart lighttpd instead of systemctl reload lighttpd as lighttpd doesn't seem to support reload using this syntax. Will that restart/reload the service as gracefully? Or would it be better to use service lighttpd reload, which does seem to be supported? Jun 14, 2018 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.