PHP Local Time – Your timezone
UPDATE: Better code. Creds for this updated code to my buddy Seth Furchgott who did the googling and grabbed the following from PHP.net, much better than what I originally posted below.
<?php
date_default_timezone_set(‘America/New_York’);
echo date(‘g:i A’);?>
Documentation for timezones:
http://us3.php.net/manual/en/timezones.php (get time zones here by Continent…and Ocean)
http://us3.php.net/manual/en/timezones.america.php (US timezones)
OLD PHP TIME POSTS (newer PHP timezone post): 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…
<?php
$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.
Comments/feedback are invited.
| Print article | This entry was posted by George on December 11, 2009 at 3:41 pm, and is filed under Web Design. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
ABOUT COMMENTING ON THIS BLOG
Comments are moderated by a human and will be approved if they bring value to the discussion. This means comments are on topic, relevant and useful. Here are some ways to insure your comment is rejected, marked as spam or will be edited (at my discretion):
- Using keywords, business names or website names in the Name field
- Putting a spammy site/page in the Website field
- Foul, abusive or discriminatory language
- Comments for SEO only purposes
about 2 years ago
hey, how will php know the value of offset? Read my article for complete references:
about 2 years ago
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: