Added the ability to change the timezone of the clock.
Previously it was only posiable to use the browers default timezone. To be done: Add more timezones to the list of possiable ones. Clean up the formating of the updating clock to match the php generated inishal date string.
This commit is contained in:
parent
a53376f57e
commit
c63c654ead
19
clock.js
19
clock.js
@ -1,4 +1,6 @@
|
||||
|
||||
/*Clock functionality. When the page is loaded it starts a one second
|
||||
* long loop to update the time displayed on the page.
|
||||
*/
|
||||
window.onload = startInterval;
|
||||
function startInterval(){
|
||||
setInterval("startTime();",1000);
|
||||
@ -6,9 +8,20 @@ function startInterval(){
|
||||
|
||||
function startTime(){
|
||||
var current_time = new Date(),
|
||||
our_tz = current_time.getTimezoneOffset,
|
||||
formated_time = current_time.getHours() + ":"
|
||||
+ current_time.getMinutes() + ":"
|
||||
+ current_time.getSeconds();
|
||||
document.getElementById('time_title').innerHTML = "Time is: " + formated_time;
|
||||
document.getElementById('time_body').innerHTML = Date();
|
||||
document.getElementById('time_title').innerHTML = "Time is: "
|
||||
+ formated_time.toLocaleString(
|
||||
{timeZone: current_tz},
|
||||
{dateStyle: "full"},
|
||||
{hour12: "false"})
|
||||
+ " " + current_tz;
|
||||
//document.getElementById('time_title').innerHTML = current_tz;
|
||||
document.getElementById('time_body').innerHTML = current_time.toLocaleString(
|
||||
{timeZone: current_tz},
|
||||
{dateStyle:"long"},
|
||||
{hour12: "false"});
|
||||
document.getElementById('JStz').innerHTML = current_tz;
|
||||
}
|
||||
|
44
clock.php
44
clock.php
@ -1,14 +1,35 @@
|
||||
<!DOCTYPE html>
|
||||
<?php
|
||||
/* Catches the form post to update the timezone.
|
||||
* If there is no form submission it will just inishilize the
|
||||
* timezone variables to be passed to the JS.
|
||||
*/
|
||||
if(isset($_POST['formSubmit'])){
|
||||
$selectedTimezone = $_POST['userTimezone'];
|
||||
if(!isset($selectedTimezone)){
|
||||
//I don't have any error message set here. It will just reload the page.
|
||||
} else{
|
||||
date_default_timezone_set($selectedTimezone);
|
||||
$scriptTZ = date_default_timezone_get();
|
||||
}
|
||||
} else{
|
||||
$selectedTimezone = date_default_timezone_get();
|
||||
//date_default_timezone_set("America/Los_Angeles");
|
||||
$scriptTZ = date_default_timezone_get();
|
||||
}
|
||||
?>
|
||||
<html>
|
||||
<head>
|
||||
<title id='time_title'>
|
||||
<?php
|
||||
echo "Time is: " . date("H:i:s");
|
||||
echo "Time is: " . date("H:i:s e");
|
||||
?>
|
||||
</title>
|
||||
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||
<meta name="author" content="efrick"/>
|
||||
<link href="/styles.css" type="text/css" rel="stylesheet"/>
|
||||
<script>var current_tz=<?php echo json_encode($scriptTZ);?></script>
|
||||
<script type="text/javascript" src="clock.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
@ -16,13 +37,30 @@
|
||||
Date and Time
|
||||
</h1>
|
||||
<div class="body">
|
||||
<?php
|
||||
<?php //Print default time and date values for the JS to update.
|
||||
echo "The full date and time:".
|
||||
"<br>".
|
||||
"<p id='time_body'>".
|
||||
date("D M d Y H:i:s") .
|
||||
" GMT" . date("O") . " (" . date("T") . ")"."</p>";
|
||||
" GMT" . date("O") . " (" . date("T") . ")"."</p>\n\t\t";
|
||||
echo '<p>The user timezone is now set to ' . $selectedTimezone . "</p>\n\t\t";
|
||||
echo '<p>The system timezone is now set to ' . $scriptTZ . "</p>\n";
|
||||
?>
|
||||
<div class="tz_select">
|
||||
<p>
|
||||
If the detcted timezone is not correct selct your timezone from the dropdown below.
|
||||
</p>
|
||||
</div>
|
||||
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
|
||||
<select name="userTimezone" class="tz_select">
|
||||
<option value="" disabled selected class="invisable">Timezone</option>
|
||||
<option value="EDT">EDT</option>
|
||||
<option value="UTC">UTC</option>
|
||||
<option value="America/Los_Angeles">PDT</option>
|
||||
</select><br/>
|
||||
<input type="submit" name="formSubmit" value="Update" >
|
||||
</form>
|
||||
<p id="JStz">JS Timezone</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
Loading…
x
Reference in New Issue
Block a user