Get the Client's Time in PHP
Find your user's local time with PHP pages.
Step 1. Any current time will do.
To get time and date on php,
First you use the “unix timestamp” ---
time();
--- to get the number of milliseconds or seconds since 1970
and it’s in Utah somewhere (UTC).
Then to get those milliseconds into a date formate with
years, months day of month, day of week, hour minute, you use:
localtime(time(), TRUE);
And this gets you the day and hour in Utah somewhere (UTC).
Step 2. Personalized time for a single page.
But to get the day and hour in your preferred time zone, there
are two options because the client doesn’t automatically tell the HTTP server
where or when the client exists.
- Either use Javascript to get the local time from the client.
- Or get the actual timezone from the client, transfer that via a new request to the server and resend the properly timed page.
Step 2.A Prepare for ancient browser with no javascript.
If the User client is without Javascript (lynx), then I put
a html form select box with all of the names from
Now, if there is not $_COOKIE[“Hour”] then there’s no javascript and the php if chain moves on to dealing with lynx by checking for $_GET[“TimeZone”]
DateTimeZone::listIdentifiers(); (:: is used
to access static (server binary) functions. )
$timezone_identifiers = DateTimeZone::listIdentifiers();
echo "<form method=\"GET\"
action=\"step20.php\"> <select name=\"TMZ\" count=5
>";
$n = count($timezone_identifiers );
for ($i=0; $i < $n ; $i++) {
echo "<option
value=\"$timezone_identifiers[$i]\">$timezone_identifiers[$i]</option><br>";
}
echo "</select><input type=\"submit\" value=\"submit\"></form>";
}
Step 2.B Get local time from Unknown Visitor with Javascript when they first reach the page .
But if the User has Javascript, then the javascript can upon loading, sends the time necessary for your purposes in a cookie and reload the page all without the client human noticing. This means the server gets two requests for the same page in the same visitation.
So that means that the php page which runs your page must
control the flow of procedures with a if-else chain depending on the either the
presence of a 1 minute cookie, or otherwise.
When php runs, if there is no 1 minute cookie then this is either the first time the client has fetched the page, or the client is unable to run Java-script as in the case of lynx or command-line browsers. If the client is able to run java-script, without checking for some sort of Java-script enabled sign, the first fetch presents the browser with a java-script firstly to get the check that document.cookie doesn’t have “Hour=x;” if it doesn’t , then this is definitely the first fetch. The <Script> sits above the <?PHP for your human readability in understanding the flow.
When php runs, if there is no 1 minute cookie then this is either the first time the client has fetched the page, or the client is unable to run Java-script as in the case of lynx or command-line browsers. If the client is able to run java-script, without checking for some sort of Java-script enabled sign, the first fetch presents the browser with a java-script firstly to get the check that document.cookie doesn’t have “Hour=x;” if it doesn’t , then this is definitely the first fetch. The <Script> sits above the <?PHP for your human readability in understanding the flow.
Var i1 = new Date();
document.cookie = “Hour=”+ i1.getHours; cookie=”Date=”+ i1.getDate();
and return as a
cookie automatically to the server’s php page, by location.reload(); because
each time the HTTP sends a request it sends the cookie too. And vice versa.
Step 3 Visitor automatically makes itself known for the same page
This location.reload
() is the second request and now the php page can find
. without a proper if-else-ifelse chain, this might work like a flip-flop. But time isn’t illogical quantum uncertainty , so we use the ifelse daisy-chain.
isset( $_COOKIE[“Hour”]
. without a proper if-else-ifelse chain, this might work like a flip-flop. But time isn’t illogical quantum uncertainty , so we use the ifelse daisy-chain.
$thishour = $_COOKIE[“Hour”];
Now, if there is not $_COOKIE[“Hour”] then there’s no javascript and the php if chain moves on to dealing with lynx by checking for $_GET[“TimeZone”]
If not that means our <form method=”get” action=”index.php”>
hasn’t sent a http://bob.com/index.php?timezone=Africa/AddisAbaba request.
So if there’s no $_COOKIE[“Hour”] and no $_GET[“TimeZONE”]
then we print simply a selection.
When there is $_GET[‘timezone’] we will get the
date_default_timezone_set($_GET["TMZ"]);
$D2 =
new DateTimeZone($_GET["TMZ"]);
$D4 =
new DateTime("now", $D2);
$todays
= localtime(date_timestamp_get($D4),1);
$thishour =
$todays["tm_hour"];
Viola, thus you have the ability for the client to send the
local time to the php server, regardless of how shocking the client is. If the
client is simply an FTP file puller like TELNET, the human can read the pulled file and find all
possible cities to choose from in the form.
(to send a adhoc Request with ?TMZ=Europe/Stockholm
This causes a negligible delay of less 1-2 seconds per visitor.
This causes a negligible delay of less 1-2 seconds per visitor.
Comments
Post a Comment