Industry guru Dave Taylor offers free tech support on a wide variety of technical and business topics, including HTML, online advertising, Cascading Style Sheets, Web design, management, Unix, Linux, search engine optimization, online dating, Mac OS X, shell script programming and Microsoft Windows.

How do I get local time with the PHP time function?

Hello I have a website and I show the time using:

<?php
\011

$fecha = time ();
\011echo "Date: " . date ( "j/m/Y" )."&nbsp;&nbsp;";
\011echo "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


Dave's Answer:

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 Stumble Upon    

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

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


Posted by: Dan at September 26, 2006 2:44 AM

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 PM

Not working because you didn't use common sense, even though the example was shot to hell from the start.

Should of read.......
$fecha = time ();
date ( "h:i A" , $time )
Instead of ......
$fecha = time ();
date ( "h:i A" , $date )

And example should of read.....
$fecha - ($offest * 60 * 60);
Instead of ......
$fecha + ($offest * 60 * 60);

Sorry for any offense.

Posted by: Al Newkirk at June 12, 2007 9:46 AM

Use this function

//Display local country time, instent of displaying server time,
//If you country is Sri Lanka and web server in in USA, you can use the following script to solve your problem
//$datetime variable shuld contain the date time like date('Y-m-d H:i:s')
//$diferent variable is the diferent between server and your local country like +5.5 -6
//$mylenth from the final out put you can tell to the function how many character from $myafter sould display
//better to keep $myafter=0 and $mylenth=0 as it is

function SafGetLocalTime($datetime,$myafter=0,$mylenth=0,$diferent){ //get local time pass datetime and diferent
list($year,$month,$day,$hour,$minute,$second) = explode("-",str_replace(array(" ",":"),"-",$datetime));
$hour = ($hour+$diferent);
$datetime = mktime($hour,$minute,$second,$month,$day,$year);

if ($mylenth==0) {
$datetime = date('Y-m-d H:i:s',$datetime);
}else{
$datetime = date('Y-m-d H:i:s',$datetime);
$datetime = substr($datetime,$myafter,$mylenth);
}
return $datetime;
}

Posted by: Safras Ahamed at May 5, 2008 12:06 AM

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();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();

Posted by: search at February 19, 2009 3:01 PM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Uniblue: Free Virus Scan

Search
Find just the answers you seek from among our 2000+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2009 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.