Page 1 of 1

current time at a location other than server

Posted: Wed Mar 25, 2009 6:23 am
by riter2k
Hello

I like to display on my webpage (in php) the "current time" at Kolkata, in India, which is in +5.5 (5 Hrs 30 Minutes ahead) timezone and the user supposed to see the "current time" in Kolkata at the moment.

According to, php.net the time() function refers to the number of seconds passed from January 1, 1970 at 00:00:00 GMT. In other words, I expect, the time() finction likely to display the number of seconds so far elapsed "till now" at GMT from Jan 1, 1970 at 00:00:00 GMT.

If my above presumption is correct, on addition of 330 minutes or 19800 (330* 60) minutes to the time(), I am supposed to get the "current time" in seconds for Kolkata from January 1, 1970 per time()+strtotime(+"330 minutes") or time(.) +strtotime(+"19800 seconds"). Accordingly, I get numeric figures (as $currenttime and $currenttime1 say) against both (time() +strtotime(+"330 minutes") and time()+strtotime(+"19800 seconds") as the number of seconds passed.

However, while converting the $currenttime to date/time per date () function [per date('l jS \of F Y h:i:s A',$currenttime) or gmdate("m/d/Y g:i:s A", $currenttime) etc] and other combinations as well, But no result matches at all with the actual time in Kolkata at any moment.

I am sure, I must be wrong somewhere but I cannot figure it out exactly where. Can anybody help me please.

Further, my server is in Florida, USA, (at Eastern Time zone GMT-5h in standard time and GMT-4h for daylight saving). Are the server time takes into account the daylight
saving or I am to consider the daylight saving for conversion or the time() is set for GMT or is there any standard function for the daylight saving (like time(), date (), gmdate() etc

Re: current time at a location other than server

Posted: Wed Mar 25, 2009 9:56 am
by Bill H
If you are using PHP 5 check out the date_default_timezone_set () function.

Servers are usually set to daylight time. Run a script echoing the date() function and you will find out.

I'm not clear what all you are saying there, but time() gives you a timestamp for the current time at your server. Add or subtract the number of seconds from that.
Don't worry about Jan 1, 1970. That date has nothing to do with anything as far as writing scripts is concerned.

Code: Select all

$s = time() + 3300;
$d = date("m/d/Y",$s);
Or whatever your offset and date format are. If the offset gives you the wrong time change it to a number that gives you the right time.

Re: current time at a location other than server

Posted: Thu Mar 26, 2009 4:37 am
by riter2k
Hello

Thanks to Bill H for your immediate reply.

Yes, your clue that "time() gives you a timestamp for the current time at your server" worked and It is not at the GMT,as referred by php.net (http://in2.php.net/time). Neither, I too was clear nor it worked.

However, I too had to make correction to your original code (as follows)
$s = time() + 3300;
$d = date("m/d/Y",$s);

The corrected one is :
<?php
$a = time();
$s = time() + 34200;
$d = date("h:m:s", $s);
echo $d."<br>";
echo "Server Date & Time".date('l jS \of F Y h:i:s A',$a)."<br>";
echo "Kolkata Date per $s".date('l jS \of F Y h:i:s A',$s)."<br>";
?>

Explanation

While Kolkata is 5 hrs 30 minutes ahead, Florida is 4 Hrs (Right now, as Daylight saving is applicable) behind the GMT. As a whole, Kolkata, right now, 9 hours 30 minutes or
34200 seconds [{(9 * 60) + 30} * 60] ahead of Florida.Therefore, 34200 seconds (Not 3300)
added to time() .

on conversion per date(), both time() at sever location (Florida Time) and (time()+34200) at
Kolkata displays the correct time, at least now (Based on -4h at Florida).

Next question :

Florida is on daylight saving (-4h) from 8th March, this year and will back to standard time (-5h) at sometime (Perhaps 2nd sunday) in November and continue till march (Perhaps 2nd sunday). However, daylight saving is not applied to Indian Time. If the server time too goes
back to standard time (-5h) in November (I cannot wait till November to cross check), the time in Kolkata on my page is very likely to differ from November to March.

Am I to take into account of it in my code itself (then it might be bit cumbersome) per if.....else... or any standardized function available for that ?

Thanks once again.

Re: current time at a location other than server

Posted: Thu Mar 26, 2009 8:49 am
by Bill H
My intentention was that you calculate your offset and date format. I assumed you could do that and you proved me right.

Check the localtime() function. if you set the is_associative flag and use the tm_isdst element of the returned array you can adjust your offset.