<?php

$system_timezone = "Europe/Berlin";
$this_document = $_SERVER["SCRIPT_NAME"];

if (!(isset($tz))) { $tz = "Europe/Berlin"; }
date_default_timezone_set($system_timezone); 
$x = localtime();
$dst =$x[8];
$offset = 1 + $dst;
$zenith=90+50/60;

$fahrenheit = 0;
$t_corr= "temp/10";
$deg_unit = "&deg;C";
$options = "";
$plotdir = "plots";


if (isset($_GET["unit"])) {
   if ($_GET["unit"] == "f") {
      $fahrenheit = 1;
      $options = "&unit=f";
      $plotdir = "plots_f";
   }
}

if ($fahrenheit) {
    $t_corr = "((temp*0.18)+32)";
    $deg_unit = "&deg;F";
} 

#$query = "select id, round($t_corr, 1) as temp, DATE_FORMAT(convert_tz(time, 'UTC', '$tz'), '%M %D, %H:%i') as ft_time, DATE_FORMAT(convert_tz(time, 'UTC', '$tz'),'%Y') AS year, DATE_FORMAT(convert_tz(time, 'UTC', '$tz'),'%m') AS month, DATE_FORMAT(convert_tz(time, 'UTC', '$tz'),'%M') AS month_name, DATE_FORMAT(convert_tz(time, 'UTC', '$tz'),'%d') AS day, YEARWEEK(convert_tz(time, 'UTC', '$tz'), 3) as yearweek from $table order by -time limit 1";

$query = "select id, round($t_corr, 1) as temp, DATE_FORMAT(time, '%M %D, %H:%i') as ft_time, DATE_FORMAT(time,'%Y') AS year, DATE_FORMAT(time,'%m') AS month, DATE_FORMAT(time,'%M') AS month_name, DATE_FORMAT(time,'%d') AS day, YEARWEEK(time, 3) as yearweek from $table order by -time limit 1";


$DB =  mysql_connect($DBHOST, $DBUSER, $DBPASS);
mysql_select_db($DBNAME, $DB);
mysql_query("SET time_zone = '$tz'") or die("MySQL error!");
$result = mysql_query($query) or die("MySQL error!");
$row = mysql_fetch_array($result);
$date = $row["ft_time"];
$temp = $row["temp"];
$month = $row["month"];
$month_name = $row["month_name"];
$day = $row["day"];
$year = $row["year"];
$max_id = $row["id"];
$day_plot = sprintf("$plotdir/temp_%d%02d%02d.png", $row["year"], $row["month"], $row["day"]);
$month_plot = sprintf("$plotdir/temp_%d%02d.png", $row["year"], $row["month"]);
$week_plot = "$plotdir/temp_" . $row["yearweek"] . "w.png";
mysql_free_result($result);


if ($show_sunrise) {
    $sunrise = date_sunrise(time() , SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset );
    $sunset = date_sunset(time() , SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
}

function ConvertOneTimezoneToAnotherTimezone($time,$currentTimezone,$timezoneRequired) {
    $system_timezone = date_default_timezone_get();
    $local_timezone = $currentTimezone;
    date_default_timezone_set($local_timezone);
    $local = date("Y-m-d h:i:s A");
    date_default_timezone_set("GMT");
    $gmt = date("Y-m-d h:i:s A");
    $require_timezone = $timezoneRequired;
    date_default_timezone_set($require_timezone);
    $required = date("Y-m-d h:i:s A");
    date_default_timezone_set($system_timezone);
    $diff1 = (strtotime($gmt) - strtotime($local));
    $diff2 = (strtotime($required) - strtotime($gmt));
    $date = new DateTime($time);
    $date->modify("+$diff1 seconds");
    $date->modify("+$diff2 seconds");
    $timestamp = $date->format("H:i");
    return $timestamp;
}


if (isset($tz)) {
   $sunrise=ConvertOneTimezoneToAnotherTimezone($sunrise,date_default_timezone_get(),$tz);
   $sunset=ConvertOneTimezoneToAnotherTimezone($sunset,date_default_timezone_get(),$tz);
}

?>