note 59276 added to function.date
| From: | karig at karig dot net | Date: | Thu, 01 Dec 2005 13:24:52 +0000 |
| Subject: | note 59276 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-99414@lists.php.net to get a copy of this message | ||
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>';
?>
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59276
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59276&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59276&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59276&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59276&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59276&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59276&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59276&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59276&report=yes
Search -- http://master.php.net/manage/user-notes.php