Php – How to get PHP date time to use that of the server

PHPphp.initimezone

My PHP scripts are currently an hour out due to BST being in operation (GMT+1).

Is there a way of setting php.ini's date.timezone to keep the time correct for BST/DST, without having to manually modify it when the clocks go back?

Alternatively, if PHP used the server's time, which is correct, that would work. Is there a way to tell it to do so?

Best Answer

Assuming reasonably-recent PHP, if you define date.timezone correctly via date_default_timezone_set(), PHP will handle the GMT offsets itself. Assuming, on the other hand, that you're stuck with old PHP, you'll have to do one of three things:

  • bite the bullet and upgrade PHP
  • bite the bullet and implement your TZ mechanics yourself (not recommended, but see here for several examples in various shades of godawful), or
  • start doing heretical things like system('date') in your code.

Of those, the clear winning solution is a PHP upgrade. How old is your version, really?

Related Topic