<?php
class Temperature
{
private $tk;
private $tc;
private $tf;
function __construct(){
$this->init_t();
}
private function init_t(){
$this->tk = 0;
$this->tc = 0;
$this->tf = 0;
}
public function set_tc($t){
$this->tk = $t + 273.15;
}
public function set_tf($t){
$this->tk = (($t - 32)*5/9)+273.15;
}
public function set_tk($t){
$this->tk = $t;
}
public function get_tc(){
return $this->tk - 273.15;
}
public function get_tf(){
return (($this->tk - 273.15)*9/5)+32;
}
public function get_tk(){
return $this->tk;
}
}
$tmp = new Temperature();
$tmp->set_tc(40);
print "If the temperaute is 40 degrees C,<br />";
$t = $tmp->get_tc();
print "Temperature in C is: $t degrees.<br />";
$t = $tmp->get_tf();
print "Temperature in F is: $t degrees.<br />";
$t = $tmp->get_tk();
print "Temperature in K is: $t degrees.<br />";
?>
myScript
Sunday, January 30, 2011
javascript - temperature conversions
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>using html5</title>
<script language="JavaScript">
<!--
function CelsiusConverter(){
var k = document.converter.celsius.value * 1 + 273.15;
TemperatureConverter(k);
}
function FahrenheitConverter(){
var k = ((document.converter.fahrenheit.value - 32)*5/9)+273.15;
TemperatureConverter(k);
}
function KelvinConverter(){
var k = document.converter.kelvin.value;
TemperatureConverter(k);
}
function TemperatureConverter(t){
document.converter.celsius.value = t - 273.15;
document.converter.fahrenheit.value= ((t - 273.15)*9/5)+32;
document.converter.kelvin.value = t;
}
-->
</script>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<!-- If the "button" gets activated, the function
in one of "text" inputs that were changed
will take the effect.
If none has been changed, nothing will happen.
If one of "text" inputs has been changed,
and if one of others, which have not been changed,
gets changed,
the function in one has been changed
will take the effect.
-->
<form name="converter">
Celsius:
<input type="text" name="celsius" onChange="CelsiusConverter()"><br />
Fahrenheit:
<input type="text" name="fahrenheit" onChange="FahrenheitConverter()"><br />
Kelvin:
<input type="text" name="kelvin" onChange="KelvinConverter()"><br />
<input type="button" value="Convert!" />
</form>
</body>
</html>
<html>
<head>
<meta charset="UTF-8">
<title>using html5</title>
<script language="JavaScript">
<!--
function CelsiusConverter(){
var k = document.converter.celsius.value * 1 + 273.15;
TemperatureConverter(k);
}
function FahrenheitConverter(){
var k = ((document.converter.fahrenheit.value - 32)*5/9)+273.15;
TemperatureConverter(k);
}
function KelvinConverter(){
var k = document.converter.kelvin.value;
TemperatureConverter(k);
}
function TemperatureConverter(t){
document.converter.celsius.value = t - 273.15;
document.converter.fahrenheit.value= ((t - 273.15)*9/5)+32;
document.converter.kelvin.value = t;
}
-->
</script>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<!-- If the "button" gets activated, the function
in one of "text" inputs that were changed
will take the effect.
If none has been changed, nothing will happen.
If one of "text" inputs has been changed,
and if one of others, which have not been changed,
gets changed,
the function in one has been changed
will take the effect.
-->
<form name="converter">
Celsius:
<input type="text" name="celsius" onChange="CelsiusConverter()"><br />
Fahrenheit:
<input type="text" name="fahrenheit" onChange="FahrenheitConverter()"><br />
Kelvin:
<input type="text" name="kelvin" onChange="KelvinConverter()"><br />
<input type="button" value="Convert!" />
</form>
</body>
</html>
Friday, January 7, 2011
javascript - date and time (2) - Imporved Version
The javascript posted below outputs the local time where the computer or client is located. If it is in England, the local time will be the England's time. If it is in New York, it will show the New York's time. The script also outputs the time in Japan, Singapore, New York, San Francisco, and Hawaii. The data will be passed to HTML in the format formXxx.datesXxx.
The javascript library doesn't give the time in the different zone areas or Coordinated Universal Time (UTC). It does, however, provide the current local time, and the offset for the local zone. Use the object of Date() class and its member fuctions ot get these two data.
UTC = the current local time + local offset
the time in different zone area = UTC + its offset
<!--
function startClock(){
// local time
document.formLoc.datesLoc.value=getTimeAndDate(13);
// san francisco time: UTF-8
var offsetSfo = -8;
document.formSfo.datesSfo.value=getTimeAndDate(offsetSfo);
// new york time: UTF-5
var offsetJfk = -5;
document.formJfk.datesJfk.value=getTimeAndDate(offsetJfk);
// hawaii time: UTF-10
var offsetHnl = -10;
document.formHnl.datesHnl.value=getTimeAndDate(offsetHnl);
// japan time: UTF+9
var offsetKix = 9;
document.formKix.datesKix.value=getTimeAndDate(offsetKix);
// singapore time: UTF+8
var offsetSin = 8;
document.formSin.datesSin.value=getTimeAndDate(offsetSin);
/* document.write(localTime); */
/* these debug stuff, cause the form outputs NOT to work.
even one of these makes it fail to display
the data in the form! */
setTimeout('startClock()',1000);
}
/*
* returns the formatted time and data
* for local time, set offset to 13
*/
function getTimeAndDate(offset){
var mS;
var time;
// 1. get the local time
var d=new Date();
// getTime() returns mS since 1970.01.01
var localTime = d.getTime();
// 2. find the local time offset
// converting minutes to mS
var localOffset=d.getTimezoneOffset()*60000;
// 3. obtain UTC
var utc = localTime + localOffset;
if((offset<=12)&&(offset>=-12)){
mS = utc + (3600000*offset); // converting mS to hours
time = new Date(mS); // new object
}
else{
time = new Date(); // new object for local
}
var year=time.getFullYear();
var month=time.getMonth();
var day=time.getDay();
var date=time.getDate();
var hour=time.getHours();
var min=time.getMinutes();
var sec=time.getSeconds();
var ampm=" ";
ampm=fixAmpm(hour);
hour=fixHour(hour);
min=fix0(min);
sec=fix0(sec);
date=fix0(date);
day=fixDay(day);
var time=
(year+"."+month+"."+date+" "+day
+" "+hour+":"+min+":"+sec+" "+ampm);
return time;
}
function fixAmpm(h){
if (h>=12){
ap="PM";
}
else{
ap="AM";
}
return ap;
}
function fix0(n){
if(n<10){
n="0"+n;
}
return n;
}
function fixHour(h){
if(h>=13){
h-=12;
}
if(h==0){
h=12;
}
if(h<10){
h=fix0(h);
}
return h;
}
function fixDay(d){
var dA;
switch(d){
case 0:
dA="SUN";
break;
case 1:
dA="MON";
break;
case 2:
dA="TUE";
break;
case 3:
dA="WED";
break;
case 4:
dA="THR";
break;
case 5:
dA="FRI";
break;
case 6:
dA="SAT"
break;
default:
dA="error";
}
return dA;
}
-->
The javascript library doesn't give the time in the different zone areas or Coordinated Universal Time (UTC). It does, however, provide the current local time, and the offset for the local zone. Use the object of Date() class and its member fuctions ot get these two data.
UTC = the current local time + local offset
the time in different zone area = UTC + its offset
<!--
function startClock(){
// local time
document.formLoc.datesLoc.value=getTimeAndDate(13);
// san francisco time: UTF-8
var offsetSfo = -8;
document.formSfo.datesSfo.value=getTimeAndDate(offsetSfo);
// new york time: UTF-5
var offsetJfk = -5;
document.formJfk.datesJfk.value=getTimeAndDate(offsetJfk);
// hawaii time: UTF-10
var offsetHnl = -10;
document.formHnl.datesHnl.value=getTimeAndDate(offsetHnl);
// japan time: UTF+9
var offsetKix = 9;
document.formKix.datesKix.value=getTimeAndDate(offsetKix);
// singapore time: UTF+8
var offsetSin = 8;
document.formSin.datesSin.value=getTimeAndDate(offsetSin);
/* document.write(localTime); */
/* these debug stuff, cause the form outputs NOT to work.
even one of these makes it fail to display
the data in the form! */
setTimeout('startClock()',1000);
}
/*
* returns the formatted time and data
* for local time, set offset to 13
*/
function getTimeAndDate(offset){
var mS;
var time;
// 1. get the local time
var d=new Date();
// getTime() returns mS since 1970.01.01
var localTime = d.getTime();
// 2. find the local time offset
// converting minutes to mS
var localOffset=d.getTimezoneOffset()*60000;
// 3. obtain UTC
var utc = localTime + localOffset;
if((offset<=12)&&(offset>=-12)){
mS = utc + (3600000*offset); // converting mS to hours
time = new Date(mS); // new object
}
else{
time = new Date(); // new object for local
}
var year=time.getFullYear();
var month=time.getMonth();
var day=time.getDay();
var date=time.getDate();
var hour=time.getHours();
var min=time.getMinutes();
var sec=time.getSeconds();
var ampm=" ";
ampm=fixAmpm(hour);
hour=fixHour(hour);
min=fix0(min);
sec=fix0(sec);
date=fix0(date);
day=fixDay(day);
var time=
(year+"."+month+"."+date+" "+day
+" "+hour+":"+min+":"+sec+" "+ampm);
return time;
}
function fixAmpm(h){
if (h>=12){
ap="PM";
}
else{
ap="AM";
}
return ap;
}
function fix0(n){
if(n<10){
n="0"+n;
}
return n;
}
function fixHour(h){
if(h>=13){
h-=12;
}
if(h==0){
h=12;
}
if(h<10){
h=fix0(h);
}
return h;
}
function fixDay(d){
var dA;
switch(d){
case 0:
dA="SUN";
break;
case 1:
dA="MON";
break;
case 2:
dA="TUE";
break;
case 3:
dA="WED";
break;
case 4:
dA="THR";
break;
case 5:
dA="FRI";
break;
case 6:
dA="SAT"
break;
default:
dA="error";
}
return dA;
}
-->
Saturday, January 1, 2011
javascript - date and time
The javascript posted below outputs the local time where the computer or client is located. If it is in England, the local time will be the England time. If it is in New York, it will show the New York time. The script also outputs the time in Japan, Singapore, New York, San Francisco, and Hawaii. The data will be passed to HTML in the format formXxx.datesXxx.
The javascript library doesn't give the time in the different zone areas or Coordinated Universal Time (UTC). It does, however, provide the current local time, and the offset for the local zone. Use the object of Date() class and its member fuctions ot get these two data.
UTC = the current local time + local offset
the time in different zone area = UTC + its offset
Below is a working javascript.
<!--
function startClock(){
// 1. get the local time
var d=new Date();
// getTime() returns mS since 1970.01.01
var localTime = d.getTime();
// 2. find the local time offset
// converting minutes to mS
var localOffset=d.getTimezoneOffset()*60000;
// 3. obtain UTC
var utc = localTime + localOffset;
// 4. get local time's offset
// local time
document.formLoc.datesLoc.value=toDestTime(0,0,1);
// san francisco time: UTF-8
var offsetSfo = -8;
document.formSfo.datesSfo.value=toDestTime(offsetSfo,utc,0);
// new york time: UTF-5
var offsetJfk = -5;
document.formJfk.datesJfk.value=toDestTime(offsetJfk,utc,0);
// hawaii time: UTF-10
var offsetHnl = -10;
document.formHnl.datesHnl.value=toDestTime(offsetHnl,utc,0);
// japan time: UTF+9
var offsetKix = 9;
document.formKix.datesKix.value=toDestTime(offsetKix,utc,0);
// singapore time: UTF+8
var offsetSin = 8;
document.formSin.datesSin.value=toDestTime(offsetSin,utc,0);
/* document.write(localTime); */
/* these debug stuff, cause the form outputs NOT to work???
even one of these makes it fail to display
the data in the form! */
setTimeout('startClock()',1000);
}
/* for local time, set i to 0 */
/* for destination time, set i to 1. */
function toDestTime(offset,u,i){
var mS;
var time;
if(i==0){
mS = u + (3600000*offset); // converting mS to hours
time = new Date(mS); // new object for destination
}
else{
time = new Date(); // new object for local
}
var year=time.getFullYear();
var month=time.getMonth();
var day=time.getDay();
var date=time.getDate();
var hour=time.getHours();
var min=time.getMinutes();
var sec=time.getSeconds();
var ampm=" ";
ampm=fixAmpm(hour);
hour=fixHour(hour);
min=fix0(min);
sec=fix0(sec);
date=fix0(date);
day=fixDay(day);
var time=
(year+"."+month+"."+date+" "+day
+" "+hour+":"+min+":"+sec+" "+ampm);
return time;
}
function fixAmpm(h){
if (h>=12){
ap="PM";
}
else{
ap="AM";
}
return ap;
}
function fix0(n){
if(n<10){
n="0"+n;
}
return n;
}
function fixHour(h){
if(h>=13){
h-=12;
}
if(h==0){
h=12;
}
if(h<10){
h=fix0(h);
}
return h;
}
function fixDay(d){
var dA;
switch(d){
case 0:
dA="SUN";
break;
case 1:
dA="MON";
break;
case 2:
dA="TUE";
break;
case 3:
dA="WED";
break;
case 4:
dA="THR";
break;
case 5:
dA="FRI";
break;
case 6:
dA="SAT"
break;
default:
dA="error";
}
return dA;
}
-->
The HTML code below shows the interface for the data (formXxx.datesXxx):
<div class="form">
<h3>Local Time</h3>
<form name="formLoc">
<input type="text" name="datesLoc" size="28">
</form>
<h3>Japan</h3>
<form name="formKix">
<input type="text" name="datesKix" size="28">
</form>
The javascript library doesn't give the time in the different zone areas or Coordinated Universal Time (UTC). It does, however, provide the current local time, and the offset for the local zone. Use the object of Date() class and its member fuctions ot get these two data.
UTC = the current local time + local offset
the time in different zone area = UTC + its offset
Below is a working javascript.
<!--
function startClock(){
// 1. get the local time
var d=new Date();
// getTime() returns mS since 1970.01.01
var localTime = d.getTime();
// 2. find the local time offset
// converting minutes to mS
var localOffset=d.getTimezoneOffset()*60000;
// 3. obtain UTC
var utc = localTime + localOffset;
// 4. get local time's offset
// local time
document.formLoc.datesLoc.value=toDestTime(0,0,1);
// san francisco time: UTF-8
var offsetSfo = -8;
document.formSfo.datesSfo.value=toDestTime(offsetSfo,utc,0);
// new york time: UTF-5
var offsetJfk = -5;
document.formJfk.datesJfk.value=toDestTime(offsetJfk,utc,0);
// hawaii time: UTF-10
var offsetHnl = -10;
document.formHnl.datesHnl.value=toDestTime(offsetHnl,utc,0);
// japan time: UTF+9
var offsetKix = 9;
document.formKix.datesKix.value=toDestTime(offsetKix,utc,0);
// singapore time: UTF+8
var offsetSin = 8;
document.formSin.datesSin.value=toDestTime(offsetSin,utc,0);
/* document.write(localTime); */
/* these debug stuff, cause the form outputs NOT to work???
even one of these makes it fail to display
the data in the form! */
setTimeout('startClock()',1000);
}
/* for local time, set i to 0 */
/* for destination time, set i to 1. */
function toDestTime(offset,u,i){
var mS;
var time;
if(i==0){
mS = u + (3600000*offset); // converting mS to hours
time = new Date(mS); // new object for destination
}
else{
time = new Date(); // new object for local
}
var year=time.getFullYear();
var month=time.getMonth();
var day=time.getDay();
var date=time.getDate();
var hour=time.getHours();
var min=time.getMinutes();
var sec=time.getSeconds();
var ampm=" ";
ampm=fixAmpm(hour);
hour=fixHour(hour);
min=fix0(min);
sec=fix0(sec);
date=fix0(date);
day=fixDay(day);
var time=
(year+"."+month+"."+date+" "+day
+" "+hour+":"+min+":"+sec+" "+ampm);
return time;
}
function fixAmpm(h){
if (h>=12){
ap="PM";
}
else{
ap="AM";
}
return ap;
}
function fix0(n){
if(n<10){
n="0"+n;
}
return n;
}
function fixHour(h){
if(h>=13){
h-=12;
}
if(h==0){
h=12;
}
if(h<10){
h=fix0(h);
}
return h;
}
function fixDay(d){
var dA;
switch(d){
case 0:
dA="SUN";
break;
case 1:
dA="MON";
break;
case 2:
dA="TUE";
break;
case 3:
dA="WED";
break;
case 4:
dA="THR";
break;
case 5:
dA="FRI";
break;
case 6:
dA="SAT"
break;
default:
dA="error";
}
return dA;
}
-->
The HTML code below shows the interface for the data (formXxx.datesXxx):
<div class="form">
<h3>Local Time</h3>
<form name="formLoc">
<input type="text" name="datesLoc" size="28">
</form>
<h3>Japan</h3>
<form name="formKix">
<input type="text" name="datesKix" size="28">
</form>
Subscribe to:
Posts (Atom)