Get server timezone In PHP

My client needs a system report his plugin running server details, It means I have to get SERVER timezone. Working with WordPress.
Tried on this, But this not working on every server.

$date = new DateTime();
$date->setTimezone(new DateTimeZone(ini_get('date.timezone')));

This gives me a fatal error on some servers,
Got any common function to get server timezone?
Any suggestions are greatly appreciated.

Related posts

Leave a Reply

6 comments

  1. <?php 
    date_default_timezone_set();
    if (date_default_timezone_get()) 
    {
        echo 'date_default_timezone_set: ' . date_default_timezone_get() . '<br />';
    }
    if (ini_get('date.timezone')) 
    {
        echo 'date.timezone: ' . ini_get('date.timezone');
    }?>
    

    this code will help you.

  2. Everything mentioned so far gives you the PHP default timezone, which may be set in php.ini or overridden somewhere in the code. If you are looking for the SERVER timezone, here is the solution:

    $timezone = $_SERVER['TZ'] ??
        (file_get_contents('/etc/timezone') ?: file_get_contents('/etc/localtime'));
    
  3. get some help from python. you can get server timezone offset with python.

    from time import gmtime, strftime
    print strftime("%z", gmtime())
    

    run the python code in php with exec

    $tz_offset = exec("python tz.py");