
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!
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.
Categorized:
CGI Scripts and Web Site Programming
(Article 5989)
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!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. 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) { I have a lot to say, but ...
I do have a comment, now that you mention it!
|
Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|