7

I am getting different dates/times between system and PHP.

System:

        $ date
        Tue Nov  3 19:16:51 EAT 2015

PHP:

        echo date("D M j G:i:s T Y");
        -> Tue Nov 3 16:16:23 UTC 2015

It appears PHP is defaulting to UTC even though the system has a different time zone. How can I make PHP pick system date/time?

Note: I am avoiding hard-coding the timezone, for maintenance reasons.

Thanks.

3

4 Answers 4

2

There seems to be no easy (system independent) way of getting system date/time in PHP, but this seems to work:

    $timezone = 'UTC';
    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
         $timezone = exec('tzutil /g');
    } else {
            $timezone = exec('date +%Z');
    }

    $localTimezone = new DateTimeZone($timezone);
    $myDateTime = new DateTime(date(), $localTimezone);
    echo ($myDateTime->format('D M j G:i:s T Y') );

I haven't tested on Windows but on Linux it works.

Sign up to request clarification or add additional context in comments.

Comments

1

Ideally, you should configure each server's php.ini where date.timezone can be set:

; ...

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

; ...

A list of acceptable timezones can be found in the PHP manual.


If you'd rather set it on each script here's a platform independent function that sets it:

<?php
function setTimezone($OSXPassword = null){

    $timezone = null;
    switch(true){
        //Linux (Tested on Ubuntu 14.04)
        case(file_exists('/etc/timezone')):
            $timezone = file_get_contents('/etc/timezone');
            $timezone = trim($timezone); //Remove an extra newline char.
            break;

        //Windows (Untested) (Thanks @Mugoma J. Okomba!)
        case(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'):
            $timezone = exec('tzutil /g');
            break;

        //OSX (Tested on OSX 10.11 - El Capitan)
        case(file_exists('/usr/sbin/systemsetup')):
            if(!isset($OSXPassword)){
                $OSXPassword = readline('**WARNING your input will appear on screen!**  Password for sudo: ');
            }
            $timezone = exec("echo '" . $OSXPassword ."' | sudo -S systemsetup -gettimezone");
            $timezone = substr($timezone, 11);
            break;
    }

    if(empty($timezone)){
        trigger_error('setTimezone could not determine your timezone', E_USER_ERROR);
    } else {
        date_default_timezone_set($timezone);
    }

    return $timezone;
}

setTimezone();
?>

As you can tell, support for OSX is sketchy.

The only decent way of getting a timezone on OSX is through systemsetup which requires super user to run. I really hate the practice of storing root's password in a PHP script but I've included the option for completeness - use with caution, this is a security vulnerability. On OSX, if root's password is not provided the script will attempt to get it from user input.

Comments

1

I just used the raw system date. Seemed like the obvious solution. Here's how I did it:

//*** OVERRIDE PHP DATE WITH RAW SYSTEM DATE ****
$sysdate = exec('date +%Y-%m-%d_%H:%M:%S');
$sysdate = str_replace("_"," ",$sysdate);
$sd = strtotime($sysdate);

$MM = date ("m", $sd);
$DD = date ("d", $sd);
$YY = date ("Y", $sd);
$HH = date ("H", $sd);
$MN = date ("i", $sd);
$SS = date ("s", $sd);

Comments

0

Try this input at the top of your file.

date_default_timezone_set('Country');

Example:
date_default_timezone_set('Hongkong');

1 Comment

This means hard-coding tmezone, which I am trying to avoid.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.