Displaying Client time with PHP

Given a PHP date/time, I’d like to adjust that time for the client’s timezone. Basically, I need a function that can take a PHP date/time and convert it to the proper client adjusted time. I DO NOT just need to know what the client’s current time is, but rather what time it happens to have been when they took a particular action on the sever but adjusted for their timezone.

I’m probably not explaining what I’m trying to achieve very well but here is some code that isn’t working. I just get the server time when I print the client time.


<?
	$serverEpoch = time();
	$clientEpoch = '<script type="text/javascript">document.write(getClientEpoch());</script>';
	$serverTime = date($format1, $serverEpoch);
	$clientTime = date($format1, $clientEpoch);	
	echo "<p>\
";
	echo "Server epoch: $serverEpoch<br />";
	echo "PHP Client epoch: $clientEpoch<br />";
	echo 'Javascript Client epoch: <script type="text/javascript">document.write(getClientEpoch());</script><br />';
	echo "Server time: $serverTime<br />";
	echo "PHP Client time: $clientTime<br />";
?>
Javascript Client time:
<script type="text/javascript">
document.write(displayDate("<?= $format1 ?>", getClientEpoch()));
</script><br />
<?
	echo "</p>\
";
?>

Basically, what I’m doing above is figuring out what the time adjustment might be for the client based on the client’s epoch value. Then I’m trying to get PHP to give me that time. You can see it NOT working here .

Just a question are you trying to find out what timezone a guest or user is in?

Hmm, your problem doesn’t have an easy solution.

First, time() doesn’t take any parameters (even if it does, it shouldn’t).

So in order to fetch the time of the client there is two solutions (that I can think of)

A) You take the clients IP, lookup what timezone via some geographic-to-IP database (just ask google). And then do your calculations based on that. However this method isn’t 100% since a user can be using a proxy that’s in another timezone, or the ip could be mismatched against the timezone)

Then we have the second alternative,

B) Send the localtime of the user by having “page-in-the-middle”.
Like this



if( !$_REQUEST["client_time"] )
{
	echo "<script type=\\"text/javascript\\">";
	echo "localtime = new Date();";
	echo "document.location.href = '$PHP_SELF?client_time=' + localtime.getTime();";
	echo "</script>";
}
else
{
	// Process page as normal
}


However, if the user for some reason has disabled JavaScript this solution won’t work either. But still I think this is the better option of the two.

Maybe there are other solutions, but these two are the ones I can think of now.

Good luck! :wink:

charlieC, thanks for your input. I updated my code on this post based on my realization that time() doesn’t take a param! Regardless, that wasn’t the problem as it still isn’t working and I can’t figure out why.

I’ll play around with your option #2 and see if I can get further.

Let me try to restate my problem as I think my code and first explanation has likely proven to be misleading…

Suppose someone uploads a document to the server and the code places a time stamp on it so when the user looks at their documents, they see all those they’ve uploaded along with a time stamp. However, it is confusing if you post a document at 07/28/04 1:00 PM, and the server is 2 hours ahead of you and so the time stamp says 07/28/04 3:00 PM.

So, I’m trying to write a function that will take the 07/28/04 3:00 PM time and adjust it for the client’s timezone. Lots of documents already posted to the site so I can’t go back and change the time stamping code to use the client’s time, I need to adjust all the legacy times on the file when I display the time stamp value.

I hope that explains things better!

Hmm ok. I might be bit slow but is this what you want?


echo "<script type=\\"text/javascript\\">";
echo "var localtime = new Date();";
echo "var unixtime = Math.floor(localtime.getTime() / 1000);";
echo "var timezone = (unixtime - " . mktime() . ");";
echo "timezone = Math.round(timezone / 3600);";
echo "document.location.href = '" . $_SERVER["PHP_SELF"] . "?timezone=' + timezone;";
echo "</script>";

Then $_REQUEST[“timezone”] will contain the time difference between the server and the client. I.e if the server is in New York and the client in London then timezone will show +5, since London is 5 hours ahead of New York.

Then this would work:


echo date("M-d-Y h:i A", mktime($file_hour, $file_minute, $file_second, $file_month, $file_day, $file_year) + $_REQUEST["timezone"]);

Edit: Fixed $PHP_SELF

Should be $_SERVER[‘PHP_SELF’]

I can boil the problem I’m having down to one thing (at least I think so!) and that is that the value stored in the following code evaluates as a string:


$clientTime = "<script type=\\"text/javascript\\">document.write(displayDate(\\"$format1\\"));</script>";

BUT, if I do this:


$convertTime = strtotime( $clientTime );

I get a value of -1 which indicates strtotime() wasn’t able to use the $clientTime variable as if it isn’t a string. I’ve tested $clientTime with is_string() and it evaluates as true.

So, any way to get that javascript built time converted into an epoch value that PHP can use with the date() function?

$clientTime = “<script type=\“text/javascript\”>document.write(displayDate(\”$format1\“));</script>”;

won’t set $clientTime to a number, it will set it to that string only. The reason it returns -1, is that it tries to convert "<script type … " to time. Therefore you get an error.

You must somehow post your client’s time back to server. That’s why you need JavaScript, like I’ve done in the examples above.