<?php
//
// 各国市場の開始終了時間と日本との時差
//
//          現地時間     時差 日本時間だと
// 日本     09:00-15:00  0    09:00-15:00
// 香港     10:00-16:00  1    11:00-17:00
// 上海     09:30-15:00  1    10:30-16:00
// インド   10:00-15:30  3.5  13:30-19:00
// ロシア   10:30-18:45  5    15:30-23:45
// トルコ   09:30-17:00  6    15:30-23:00
// ロンドン 08:00-16:30  8    16:00-24:30
// ブラジル 10:00-17:00  12   22:00-05:00
// アメリカ 09:30-16:00  13   22:30-05:00
//
// 初期値設定
//
$dbserver = "localhost";
$dbuser = "root";
$dbpassword = "hogehoge";
$dbname = "kabu";
$con=mysql_connect($dbserver,$dbuser,$dbpassword);
$selectdb=mysql_select_db($dbname);
$nowtime=date("Hi");
$masterdate = -365;



//
// 市場開始時間以前だったら 2日前をセット
// 市場開始時間以降だったら前日をセット
//
if ($nowtime < 900){$dateoffset = $masterdate -1;
}else{
$dateoffset = $masterdate;}

//
// 本日の最後の価額を取得
//
$sql="select * from toushin where name='N225' and kagaku>0 order by date desc,time desc,id desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$today="$col[kagaku]";

//
// $dateoffset変数の日付分日数をさかのぼったその日の最後の価額を取得
//
$sql="select * from toushin where date between date_add(current_date(),interval $dateoffset day) and current_date() and name='N225' order by kagaku desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$yesterday="$col[kagaku]";
$yesterday=round($yesterday,0);

//
// 騰落の計算。小数点以下 1ケタ
//
$touraku = ($yesterday==0) ? 0 : sprintf("%01.1f",(($today/$yesterday)-1)*100);
$today=round($today,0);

//
// 上下の矢印を付ける
//
if ($touraku >= 0){
$touraku = "<font color=red>↑+$touraku</font>";
}else{
$touraku = "<font color=green>↓$touraku</font>";
}

//
// html部分でページ部分となる $body変数へ、htmlのページ構成の通りに吐き出し
//
$body = "<pre>過去1年最高値 本日   騰落
N225   $yesterday $today $touraku%
";

//
// 以下 N225と同じ処理を繰り返す
//
if ($nowtime < 900){$dateoffset = $masterdate -1;
}else{
$dateoffset = $masterdate;}

$sql="select * from toushin where name='TOPIX' and kagaku>0 order by date desc,time desc,id desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$today="$col[kagaku]";
$sql="select * from toushin where date between date_add(current_date(),interval $dateoffset day) and current_date() and name='TOPIX' order by kagaku desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$yesterday="$col[kagaku]";
$touraku = ($yesterday==0) ? 0 : sprintf("%01.1f",(($today/$yesterday)-1)*100);
$yesterday=round($yesterday,0);
$today=round($today,0);
if ($touraku >= 0){
$touraku = "<font color=red>↑+$touraku</font>";
}else{
$touraku = "<font color=green>↓$touraku</font>";
}
$body .="TOPIX   $yesterday  $today $touraku%
";


if ($nowtime < 900){$dateoffset = $masterdate -1;
}else{
$dateoffset = $masterdate;}

$sql="select * from toushin where name='USD' and kagaku>0 order by date desc,time desc,id desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$today="$col[kagaku]";
$sql="select * from toushin where date between date_add(current_date(),interval $dateoffset day) and current_date() and name='USD' order by kagaku desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$yesterday="$col[kagaku]";
$touraku = ($yesterday==0) ? 0 : sprintf("%01.1f",(($today/$yesterday)-1)*100);
$yesterday=sprintf("%01.1f",round($yesterday,1));
$today=sprintf("%01.1f",round($today,1));
if ($touraku >= 0){
$touraku = "<font color=red>↑+$touraku</font>";
}else{
$touraku = "<font color=green>↓$touraku</font>";
}
$body .="USD    $yesterday $today $touraku%
";


if ($nowtime < 1030){$dateoffset = $masterdate -1;
}else{
$dateoffset = $masterdate;}

$sql="select * from toushin where name='000001.ss' and kagaku>0 order by date desc,time desc,id desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$today="$col[kagaku]";

$sql="select * from toushin where date between date_add(current_date(),interval $dateoffset day) and current_date() and name='000001.ss' order by kagaku desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$yesterday="$col[kagaku]";
$touraku = ($yesterday==0) ? 0 : sprintf("%01.1f",(($today/$yesterday)-1)*100);
$yesterday=round($yesterday,0);
$today=round($today,0);
if ($touraku >= 0){
$touraku = "<font color=red>↑+$touraku</font>";
}else{
$touraku = "<font color=green>↓$touraku</font>";
}
$body .="上海A   $yesterday  $today $touraku%
";


$sql="select * from toushin where name='000016.ss' and kagaku>0 order by date desc,time desc,id desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$today="$col[kagaku]";
$sql="select * from toushin where date between date_add(current_date(),interval $dateoffset day) and current_date() and name='000016.ss' order by kagaku desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$yesterday="$col[kagaku]";
$touraku = ($yesterday==0) ? 0 : sprintf("%01.1f",(($today/$yesterday)-1)*100);
$yesterday=round($yesterday,0);
$today=round($today,0);
if ($touraku >= 0){
$touraku = "<font color=red>↑+$touraku</font>";
}else{
$touraku = "<font color=green>↓$touraku</font>";
}
$body .="SSE50   $yesterday  $today $touraku%
";


if ($nowtime < 1100){$dateoffset = $masterdate -1;
}else{
$dateoffset = $masterdate;}

$sql="select * from toushin where name='HSI' and kagaku>0 order by date desc,time desc,id desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$today="$col[kagaku]";
$sql="select * from toushin where date between date_add(current_date(),interval $dateoffset day) and current_date() and name='HSI' order by kagaku desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$yesterday="$col[kagaku]";
$touraku = ($yesterday==0) ? 0 : sprintf("%01.1f",(($today/$yesterday)-1)*100);
$yesterday=round($yesterday,0);
$today=round($today,0);
if ($touraku >= 0){
$touraku = "<font color=red>↑+$touraku</font>";
}else{
$touraku = "<font color=green>↓$touraku</font>";
}
$body .="ハンセン   $yesterday $today $touraku%
";

$sql="select * from toushin where name='HSCE' and kagaku>0 order by date desc,time desc,id desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$today="$col[kagaku]";
$sql="select * from toushin where date between date_add(current_date(),interval $dateoffset day) and current_date() and name='HSCE' order by kagaku desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$yesterday="$col[kagaku]";
$touraku = ($yesterday==0) ? 0 : sprintf("%01.1f",(($today/$yesterday)-1)*100);
$yesterday=round($yesterday,0);
$today=round($today,0);
if ($touraku >= 0){
$touraku = "<font color=red>↑+$touraku</font>";
}else{
$touraku = "<font color=green>↓$touraku</font>";
}
$body .="H株    $yesterday $today $touraku%
";


if ($nowtime < 1330){$dateoffset = $masterdate -1;
}else{
$dateoffset = $masterdate;}


$sql="select * from toushin where name='SENSEX' and kagaku>0 order by date desc,time desc,id desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$today="$col[kagaku]";
$sql="select * from toushin where date between date_add(current_date(),interval $dateoffset day) and current_date() and name='SENSEX' order by kagaku desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$yesterday="$col[kagaku]";
$touraku = ($yesterday==0) ? 0 : sprintf("%01.1f",(($today/$yesterday)-1)*100);
$yesterday=round($yesterday,0);
$today=round($today,0);
if ($touraku >= 0){
$touraku = "<font color=red>↑+$touraku</font>";
}else{
$touraku = "<font color=green>↓$touraku</font>";
}
$body .="SENSEX $yesterday $today $touraku%
";


if ($nowtime < 1530){$dateoffset = $masterdate -1;
}else{
$dateoffset = $masterdate;}

$sql="select * from toushin where name='RTS' and kagaku>0 order by date desc,time desc,id desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$today="$col[kagaku]";
$sql="select * from toushin where date between date_add(current_date(),interval $dateoffset day) and current_date() and name='RTS' order by kagaku desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$yesterday="$col[kagaku]";
$touraku = ($yesterday==0) ? 0 : sprintf("%01.1f",(($today/$yesterday)-1)*100);
$yesterday=round($yesterday,0);
$today=round($today,0);
if ($touraku >= 0){
$touraku = "<font color=red>↑+$touraku</font>";
}else{
$touraku = "<font color=green>↓$touraku</font>";
}
$body .="RTS     $yesterday  $today $touraku%
";


if ($nowtime < 1530){$dateoffset = $masterdate -1;
}else{
$dateoffset = $masterdate;}

$sql="select * from toushin where name='XU100' and kagaku>0 order by date desc,time desc,id desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$today="$col[kagaku]";
$sql="select * from toushin where date between date_add(current_date(),interval $dateoffset day) and current_date() and name='XU100' order by kagaku desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$yesterday="$col[kagaku]";
$touraku = ($yesterday==0) ? 0 : sprintf("%01.1f",(($today/$yesterday)-1)*100);
$yesterday=round($yesterday,0);
$today=round($today,0);
if ($touraku >= 0){
$touraku = "<font color=red>↑+$touraku</font>";
}else{
$touraku = "<font color=green>↓$touraku</font>";
} 
$body .="XU100  $yesterday $today $touraku%
";


if ($nowtime < 1600){$dateoffset = $masterdate;
}else{
$dateoffset = $masterdate + 1;}

$sql="select * from toushin where name='FTSE' and kagaku>0 order by date desc,time desc,id desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$today="$col[kagaku]";
$sql="select * from toushin where date between date_add(current_date(),interval $dateoffset day) and current_date() and name='FTSE' order by kagaku desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$yesterday="$col[kagaku]";
$touraku = ($yesterday==0) ? 0 : sprintf("%01.1f",(($today/$yesterday)-1)*100);
$yesterday=round($yesterday,0);
$today=round($today,0);
if ($touraku >= 0){
$touraku = "<font color=red>↑+$touraku</font>";
}else{
$touraku = "<font color=green>↓$touraku</font>";
}
$body .="FTSE    $yesterday  $today $touraku%
";


if ($nowtime < 2230){$dateoffset = $masterdate;
}else{
$dateoffset = $masterdate + 1;}

$sql="select * from toushin where name='DJI' and kagaku>0 order by date desc,time desc,id desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$today="$col[kagaku]";
$sql="select * from toushin where date between date_add(current_date(),interval $dateoffset day) and current_date() and name='DJI' order by kagaku desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$yesterday="$col[kagaku]";
$touraku = ($yesterday==0) ? 0 : sprintf("%01.1f",(($today/$yesterday)-1)*100);
$yesterday=round($yesterday,0);
$today=round($today,0);
if ($touraku >= 0){
$touraku = "<font color=red>↑+$touraku</font>";
}else{
$touraku = "<font color=green>↓$touraku</font>";
}
$body .="DJI    $yesterday $today $touraku%
";


$sql="select * from toushin where name='EWZ' and kagaku>0 order by date desc,time desc,id desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$today="$col[kagaku]";
$sql="select * from toushin where date between date_add(current_date(),interval $dateoffset day) and current_date() and name='EWZ' order by kagaku desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$yesterday="$col[kagaku]";
$touraku = ($yesterday==0) ? 0 : sprintf("%01.1f",(($today/$yesterday)-1)*100);
$yesterday=sprintf("%01.1f",round($yesterday,1));
$today=sprintf("%01.1f",round($today,1));
if ($touraku >= 0){
$touraku = "<font color=red>↑+$touraku</font>";
}else{
$touraku = "<font color=green>↓$touraku</font>";
}
$body .="EWZ     $yesterday  $today $touraku%
";


if ($nowtime < 2200){$dateoffset = $masterdate;
}else{
$dateoffset = $masterdate + 1;}

$sql="select * from toushin where name='BVSP' and kagaku>0 order by date desc,time desc,id desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$today="$col[kagaku]";
$sql="select * from toushin where date between date_add(current_date(),interval $dateoffset day) and current_date() and name='BVSP' order by kagaku desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$yesterday="$col[kagaku]";
$touraku = ($yesterday==0) ? 0 : sprintf("%01.1f",(($today/$yesterday)-1)*100);
$yesterday=round($yesterday,0);
$today=round($today,0);
if ($touraku >= 0){
$touraku = "<font color=red>↑+$touraku</font>";
}else{
$touraku = "<font color=green>↓$touraku</font>";
}
$body .="BVSP   $yesterday $today $touraku%

";


if ($nowtime < 900){$dateoffset = $masterdate -1;
}else{
$dateoffset = $masterdate;}

$sql="select * from toushin where name='9205' and kagaku>0 order by date desc,time desc,id desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$today="$col[kagaku]";
$sql="select * from toushin where date between date_add(current_date(),interval $dateoffset day) and current_date() and name='9205' order by kagaku desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$yesterday="$col[kagaku]";
$touraku = ($yesterday==0) ? 0 : sprintf("%01.1f",(($today/$yesterday)-1)*100);
$yesterday=round($yesterday,0);
$today=round($today,0);
if ($touraku >= 0){
$touraku = "<font color=red>↑+$touraku</font>";
}else{
$touraku = "<font color=green>↓$touraku</font>";
}
$body .="JAL      $yesterday   $today $touraku%
";


$sql="select * from toushin where name='9202' and kagaku>0 order by date desc,time desc,id desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$today="$col[kagaku]";
$sql="select * from toushin where date between date_add(current_date(),interval $dateoffset day) and current_date() and name='9202' order by kagaku desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$yesterday="$col[kagaku]";
$touraku = ($yesterday==0) ? 0 : sprintf("%01.1f",(($today/$yesterday)-1)*100);
$yesterday=round($yesterday,0);
$today=round($today,0);
if ($touraku >= 0){
$touraku = "<font color=red>↑+$touraku</font>";
}else{
$touraku = "<font color=green>↓$touraku</font>";
}
$body .="ANA      $yesterday   $today $touraku%
";


$sql="select * from toushin where name='7762' and kagaku>0 order by date desc,time desc,id desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$today="$col[kagaku]";
$sql="select * from toushin where date between date_add(current_date(),interval $dateoffset day) and current_date() and name='7762' order by kagaku desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$yesterday="$col[kagaku]";
$touraku = ($yesterday==0) ? 0 : sprintf("%01.1f",(($today/$yesterday)-1)*100);
$yesterday=round($yesterday,0);
$today=round($today,0);
if ($touraku >= 0){
$touraku = "<font color=red>↑+$touraku</font>";
}else{
$touraku = "<font color=green>↓$touraku</font>";
}
$body .="シチズン    $yesterday   $today $touraku%
";


$sql="select * from toushin where name='1309' and kagaku>0 order by date desc,time desc,id desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$today="$col[kagaku]";
$sql="select * from toushin where date between date_add(current_date(),interval $dateoffset day) and current_date() and name='1309' order by kagaku desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$yesterday="$col[kagaku]";
$touraku = ($yesterday==0) ? 0 : sprintf("%01.1f",(($today/$yesterday)-1)*100);
$yesterday=round($yesterday,0);
$today=round($today,0);
if ($touraku >= 0){
$touraku = "<font color=red>↑+$touraku</font>";
}else{
$touraku = "<font color=green>↓$touraku</font>";
}
$body .="1309   $yesterday $today $touraku%
";


$sql="select * from toushin where name='2805' and kagaku>0 order by date desc,time desc,id desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$today="$col[kagaku]";
$sql="select * from toushin where date between date_add(current_date(),interval $dateoffset day) and current_date() and name='2805' order by kagaku desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$yesterday="$col[kagaku]";
$touraku = ($yesterday==0) ? 0 : sprintf("%01.1f",(($today/$yesterday)-1)*100);
$yesterday=round($yesterday,0);
$today=round($today,0);
if ($touraku >= 0){
$touraku = "<font color=red>↑+$touraku</font>";
}else{
$touraku = "<font color=green>↓$touraku</font>";
}
$body .="SBショクヒン  $yesterday   $today $touraku%
";

//
// mysql最後の idの日付、時間を最終更新時間として吐き出し
//
$sql="select * from toushin order by id desc limit 1";
$rst = mysql_query($sql);
$col = mysql_fetch_array($rst);
$lastid="$col[id]";
$date2=substr($col[date],5,strlen($col[date])-5);
$time2=substr($col[time],0,strlen($col[time])-3);
$body2 ="
最終更新 $date2 $time2 $lastid";

//
// mysqlを閉じる
//
mysql_free_result($rst);
$con = mysql_close($con);

//
// PHPの処理終了。以下 htmlのページ作成部分
//
?>

<html><head><meta name=CHTML><meta http-equiv=Content-Type content="text/html; charset=EUC-JP"><title>携帯で指数ページ</title></head><body bgcolor=black text=white><?=$body?><?=$body2?><a href=index.html><? include "/home/localhost/c_count/counter.inc"; ?><? echo $counter ?></a></pre></body></html>