Can someone explain why the following doesn't work?
I have a PHP script that runs on a shared hosting account. The script should output my local time (for the time zone where I'm located, i.e. GMT-8:00
, or PST
.)
I'm reading documentation for time() PHP method that is supposed to return a Unix timestamp in UTC (or GMT). OK, good. So I need to subtract 8 hrs to get my local time, right?
So I'm doing this:
echo(date("n/j/Y, g:i:s A", time() - 28800)); //8hrs = 28800 seconds
But I'm getting time that is 5 hrs behind!
What can be wrong in a one-line statement as such?
PS. Sorry, if I'm asking the obvious. I'm coming from the world of .NET
. I'm new to PHP.
PHP's time() function will always return seconds since the epoch. The culprit for getting unexpected behavior is actually the date() function.
PHP's date() function will automatically convert the date to the current default timezone of PHP, which must be GMT - 3. This is why when you subtract 8 hours you're 5 hours behind.
Instead of using date(), you probably want to check in to gmdate(). Something like:
may be what you need.