|
How do I get local time with the PHP time function?Hello I have a website and I show the time using: <?php $fecha = time (); date ( "h:i A" , $date ) But the time in my webserver is different from my time and I wanna display my time in my website, how can I modify the time? for example if the time in my server says 5pm i want to show 6pm I'm not much of a PHP expert, so I turned to my friend and colleague Derek Scruggs of Escalan. Here's what he shared: PHP's time() function returns a timestamp of the number of seconds since Jan 1, 1970. The date() function uses that timestamp in conjunction with the time zone of the server to display the timestamp into a human-readable value. If you want to use a different time zone when converting the timestamp, simply add or subtract the number of seconds differing between your server's time zone and your preferred time zone to the timestamp. So if you live in Denver (Mountain Time) and your server is in New York (Eastern Time), you'll need to subtract 7200 seconds (2 hours = 120 minutes = 7200 seconds). Just insert this code right after the $fecha=time() section. /*$offset is the number of hours between Mountain and Eastern. Change this to whatever the difference is between your server's time and the preferred time zone. It's a negative number if you are behind the server time, and a positive number if you are ahead of it.*/ $offset= -2; $fecha = $fecha + ($offest * 60 * 60); //multiply offset times 60 minutes times 60 seconds to get the total difference in seconds //continue with rest of code as before... Hope that helps you figure this out. Thanks, Derek!
Categorized:
CGI Scripts and Web Site Programming
(Article 5989,
Written by Dave Taylor)
Tagged: php, php localtime, php time function Previous: How do I format SD memory on my RCA Lyra MP3 Player? Next: Can't pair my Oakley RAZRWIRE with my Moto RAZR V3 phone? Subscribe!
You can use this PHP5 (>= 5.1) function: date_default_timezone_set('Europe/Zurich'); Replace 'Europe/Zurich' with the param. for your timezone (see them here: http://ch2.php.net/manual/en/timezones.php ). Put this func. at the top of your script. All subsequent date/time calculations will be based on that timezone. Simple and efficient. HTH, Dan You have a typo in: "$fecha = $fecha + ($offest * 60 * 60); //multiply offset times 60 minutes times 60 seconds to get the total difference in seconds" "offest" should equal "offset" Jeez, even after I did that it's still not working for me.... Posted by: Robert at February 20, 2007 6:55 PMNot working because you didn't use common sense, even though the example was shot to hell from the start. Should of read....... And example should of read..... Sorry for any offense. Use this function //Display local country time, instent of displaying server time, function SafGetLocalTime($datetime,$myafter=0,$mylenth=0,$diferent){ //get local time pass datetime and diferent if ($mylenth==0) { If you want a fix local time, you can add or minus the hour you get from date(). Or if you want local pc time whoever go to the page, you need javascript. var today=new Date(); You need not think more just add number of second you want add with GMT time Example: $time=date("h:i A", time()+25200); //GMT + 7 hours Posted by: A.T.M. Alaul Haque at October 31, 2009 9:55 PMI realize this is an old post, but thought I'd put my 2 cents in... I believe this is what A.T.M. Alaul Haque was getting at, but to put it a little more clearly, always reference your date in regards to GMT and you don't have to worry about the server. If you switch servers it will still work: $timezone_offset = -6; // us central time (gmt-6) for me $date = gmdate('M d Y H:i:s', time()+$timezone_offset*60*60); Posted by: Soylent at January 23, 2010 12:55 PMThis all looks good. Anyone know how to call the users local time, based on a set time? For example: I am in england and lunch time is 1:00 PM. I want to tell a user in California or New York, etc. that lunch time for me is 1:00 PM England, but in the users local time. Any suggestions? Ray Posted by: Ray at March 5, 2010 4:38 PMI have something to say, now that you mention it, but ...
I do have a comment, now that you mention it!
|
Recommended
Recent Entries
Search
I Need Help!
Apple iPad Help
Articles and Reviews Auctions and Online Shopping Blogs and RSS Feeds Building Web Site Traffic Business and Management CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above Facebook Help Google Plus Help HTML and CSS Industry News and Trade Shows iPhone and Cell Phone Help iPod, Sony PSP and MP3 Player Help Mac OS X Help Pay Per Click (PPC) Advertising Search Engine Optimization (SEO) Shell Script Programming Tech Support Video Help The Writing Business Twitter, LinkedIn and Social Network Help Unix and Linux Help Video Game Tips and Help Windows PC Help WordPress Help |