coolpup
coolpup

Reputation: 159

Timezone not changing after using date_default_timezone_set

I'm setting my default time zone for my page using:

date_default_timezone_set("America/Los_Angeles");

I have to set it there, because my server doesn't allow me to alter the php.ini file or .htaccess. The problem is, when I use this:

NOW()

to send the current time to my database, it still send it as the UTC timezone.

What I'm trying to do is display comments users display from a comment box on the page, and it's showing the time for each of the comments in the wrong timezone now.

Upvotes: 0

Views: 879

Answers (2)

Niels Abildgaard
Niels Abildgaard

Reputation: 3000

date_default_timezone_set is a PHP function. It can only affect the behaviour of PHP.

NOW() is a database function, and changing your timezone in PHP has no effect on it. NOW() returns in the format YYYY-MM-DD HH:MM:SS.

time() is the equivalent PHP function. time() returns simply the number of seconds since the Unix Epoch. To get output in the same format as NOW(), use date("Y-m-d H-i-s");. This automatically uses time() underneath to get the current system time.

Read more:

Upvotes: 1

dayuloli
dayuloli

Reputation: 17051

From the manual

date_default_timezone_set — Sets the default timezone used by all date/time functions in a script

Thus, use the date_default_timezone_set function only affects those functions. You should instead use time() instead.

Upvotes: 0

Related Questions