note 59276 deleted from function.date by betz
| From: | betz@php.net | Date: | Fri, 02 Dec 2005 18:04:08 +0000 |
| Subject: | note 59276 deleted from function.date by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-99473@lists.php.net to get a copy of this message | ||
Note Submitter: karig at karig dot net
----
IMPORTANT: UPDATE TO MY PREVIOUS POST about using Javascript to send the site visitor's local
time to your server:
My Javascript was not as reliable as I thought it was. It seems that date.toString() might mean
different things to different browsers. So I rewrote the Javascript to force the creation of a
string with a fixed format.
This is the Javascript that should have gone into "timestamp_from_client.php":
<script language="javascript">
<!--
function pad(number,length) {
var str = '' + number;
while (str.length < length)
str = '0' + str;
return str;
}
function timestampTheForm(form) {
//Sets timestamp to format: "yyyy-mm-dd hh:mm:ss (n)"
//where 'hh' is 00..23 and 'n' is the timezone offset
//from GMT/UTC in number of minutes.
d = new Date();
s = pad(d.getFullYear(), 4);
s += '-' + pad(d.getMonth(), 2);
s += '-' + pad(d.getDate(), 2);
s += ' ' + pad(d.getHours(), 2);
s += ':' + pad(d.getMinutes(), 2);
s += ':' + pad(d.getSeconds(), 2);
s += ' (' + d.getTimezoneOffset() + ')';
form.timestamp.value = s;
}
//-->
</script>
And this is the new PHP code to handle the conversion of a timezone offset into "GMT+nnnn"
format -- code that should have gone into "timestamp_to_server.php":
<?php
$ts = $_POST['timestamp'];
echo '<p>My server (via PHP) says the time here is:<br />'
. date('l j F Y : g:i:s A (T)') . '</p>';
echo '<p>Your computer (via Javascript) says the time there is:<br />'
. $ts . '</p>';
$pos = strpos($ts, '(') + 1;
$len = strlen($ts) - $pos - 1;
$zone = substr($ts, $pos, $len);
$z1 = sprintf("%+03d", -($zone / 60));
$z2 = sprintf("%02d", abs($zone % 60));
$zone = 'GMT' . $z1 . $z2;
$ts = date('l j F Y : g:i:s A (', strtotime(substr($ts, 0, 19))) . $zone . ')';
echo '<p>I can now format your local time like this:<br />'
. $ts . '</p>';
?>