PHP Local Time – Your timezone
UPDATE: After a comment from a visitor I re-checked my code, did some testing and added the echo statement with formatted time, and improved the comments about how to check for the correct value of $offset. Also, note I have not used this code for a year yet so I do not know if daylight savings has been taken into account.
PHP code to get time in YOUR timezone...
<?
$mydatetime = time ();
$offset= 0; // offset in # of hours from desired location
// check the time displayed using $offset = 0 and adjust
// the offset accordingly
$mydatetime = $mydatetime + ($offset * 60 * 60);
echo date ("g:i A", $mydatetime);?>
Check the PHP Time manual page for formatting the time. This code has only been tested on two implementations. When I wanted to display the local time for a website's "offical time" I couldn't find any simple explanations online.
Comments/feedback are invited.
hey, how will php know the value of offset? Read my article for complete references:
Good point @rana... the code above is set to the time zone of the server in use. The offset is adjusted by comparing the time displayed (using the offset of zero) to the time you want to appear. I live and work in the Eastern Standard Time (EST, same as New York, USA) and EST is the time zone for the sites I usually build. As mentioned in the post above I know it works on at least the one implementation I've used because the time was "set" the that day I wrote the post.
This is the code I ended up with this to get EST: