get server time in php - timezone issues

196

on shell, my server time is (simpledate in bash): Sun Aug 29 10:37:12 EDT 2010

when I run the codephp -r "echo date('Y-m-d H:i:s');" I get: 2010-08-29 10:37:10

but when I execute a php script that hasecho date('Y-m-d H:i:s'); I get a different time- since php thinks the timezone is UTC (server time is EDT).

My php.ini file has no timezone set, and I wouldnt like to change anything- as I have no idea what affect it would have on other sites that run on this server.

Is there a simple way to get the server time- at the timezone that is set for the server?

787

Answer

Solution:

The server should always have a timezone specified in thephp.ini file. PHP 5.3 even raises notices if that's not the case and theDateTime extension will throw exceptions if you attempt to build objects without explicitly specifying a timezone (with aDateTimezone object) or having a default one (either throughdate.timezone ordate_default_timezone_set).

By what you're describing, the most likely scenarios are:

  • Different php.ini files are being used in the two scenarios. In the first case, no default timezone is specified, so PHP attempts to infer one from the system-wide timezone of the machine.
  • The (second) script callsdate_default_timezone_set('UTC') or equivalent.
492

Answer

Solution:

Late for the party, but if you want to set the timezone without edit php.ini, you can put in the php code one of these strings:

date_default_timezone_set('GMT');
date_default_timezone_set('Europe/Zurich');
date_default_timezone_set('America/New_York');

People are also looking for solutions to the problem: How do you use php to communicate with App Inventors TinyWebDB component?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.