note 59264 deleted from function.date by betz
| From: | betz@php.net | Date: | Fri, 02 Dec 2005 18:04:03 +0000 |
| Subject: | note 59264 deleted from function.date by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-99472@lists.php.net to get a copy of this message | ||
Note Submitter: karig at karig dot net
----
Don't mess around with time-zone scripts; use Javascript to tell you what time it is where your
website visitor lives.
Dates in PHP use two time zones: GMT and the time zone of the server. If you want PHP to deal with
times set in the time zone of a visitor to your website, such as the time when he posts a comment to
your blog -- well, PHP isn't really set up for that. For one thing, PHP doesn't seem to
have provisions for converting a date freely from one timezone to another, and for another thing,
PHP is a server-side language and can't check the time on your visitor's computer.
If you need to do something on your visitor's computer, you reach for a client-side language,
like Javascript. I have attached two demo web pages below. The first one has a form (well, a
"submit" button) that calls a Javascript function to send the client's local time to
the server. The second one receives the Javascript time string, and of course you can use PHP to
manipulate the string, store it, convert it, and so on.
I like this solution because there is no code needed to try to deal with "summer time" or
"daylight savings time"; there is no need to ask the visitor to tell you that the time
zone he lives in is "GMT-0500" or "GMT-0700" or something else; etc. A form that
allows a visitor to leave a comment can simply send the local time unobtrusively to the server, and
a script on the server can extract the time zone from this time string and use it to alter the
display of other times on the website automatically, for the benefit of the visitor.
========================================
FIRST PAGE: "timestamp_from_client.html"
<html><body>
<p>
Your computer and my server might be in different time zones. My server wants to use dates from its
own time zone, but you're going to want to use dates from yours.
</p>
<p>
Click on the button below to send the time on your computer to my server. (PHP, which runs on my
server, doesn't know what time it is where you are. Javascript, which runs on your computer,
DOES know what time it is there and can send that information here.)
</p>
<script language="javascript">
<!--
function timestampTheForm(form) {
d = new Date();
s = d.toString();
form.timestamp.value = s;
}
//-->
</script>
<form name="the_form" action="timestamp_to_server.php"
method="post">
<input type="hidden" name="timestamp" value="time">
<input type="submit" value="Submit"
onClick="timestampTheForm(this.form)";>
</body></html>
========================================
SECOND PAGE: "timestamp_to_server.php"
<html>
<body>
<?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>';
$zone = substr($ts, strpos($ts, 'GMT'), 8);
$ts = date('l j F Y : g:i:s A (', strtotime(substr($ts, 0, 25))) . $zone . ')';
echo '<p>I can now format your local time like this:<br />'
. $ts . '</p>';
?>
</body>
</html>