There are many answers, but at the time of writing the letter was not even correct.
The PHP time() function does not return the system time, as most people believe, but returns the local PHP time, usually set using date.timezone in php.ini or setting with date_default_timezone_set() in a script.
For example, on one of my servers the PHP time was Europe/Rome and the UTC system time. I had an hour difference between system time and PHP time.
I am going to give you a solution that works for Linux, I donβt know for Windows. On Linux, the system time zone is set to /etc/timezone . Now this is usually outside the valid open_basedir parameter, but you can add :/etc/timezone to your list to read the file.
Then on top of scripts that want to get the system time, you can call the library function, which sets the script time zone to the system time zone. I believe this function is part of the class, so I use static:
static function setSystemTz() { $systemTz = trim(file_get_contents("/etc/timezone")); if ($systemTz == 'Etc/UTC') $systemTz = 'UTC'; date_default_timezone_set($systemTz); }
To make matters worse in PHP 5.3.3, βEtc / UTCβ is not recognized, while βUTCβ is, so I had to add an if to fix this.
Now you can happily call time() , and that will really give you the system time. I tested it because I needed it for myself, so I found this question now.
stivlo
source share