<?php

function xml_attribute($object, $attribute)
{
    if(isset($object[$attribute]))
        return (string) $object[$attribute];
}

    $data_file="http://api.pogoda.com/index.php?api_lang=ru&localidad=13564&affiliate_id=qkm6r626snmm&v=2.0"; // адрес xml файла 
	//http://api.pogoda.com/index.php?api_lang=ru&localidad=13564&affiliate_id=qkm6r626snmm
	//http://api.pogoda.com/index.php?api_lang=ru&localidad=242264&affiliate_id=qkm6r626snmm&v=2
	//http://api.pogoda.com/index.php?id=qkm6r626snmm&r=ac 
    $city_id=13564; // id города
        $xml = simplexml_load_file($data_file); // раскладываем xml на массив
		//print_r($xml);
    // выбираем требуемые параметры (город, температура, пиктограмма и тип погоды текстом (облачно, ясно)
    //    https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="leninsk-kuznetskiy' ru")&format=xml&env=store://datatables.org/alltableswithkeys
    //Смотреть Yahoo здесь: https://developer.yahoo.com/weather/?dataTypeRadios=XML
    //А это Рамблер: http://api.rambler.ru/weather/informer?content_type=xml 

$city=$xml->location[city];

    //$time=$xml->fact->observation_time;
//    $time_t=$xml->location->day[1];
	//	echo [location][day][1][hour]
    
$date = date("H");
	
if ($date >= 23 and $date < 3) {
    var_dump($wind=$xml->location->day[1]->hour[0]);
} elseif ($date >= 3 and $date < 6) {
    var_dump($wind=$xml->location->day[1]->hour[1]);
} elseif ($date >= 6 and $date < 9) {
    var_dump($wind=$xml->location->day[1]->hour[2]);
} elseif ($date >= 9 and $date < 11) {
    var_dump($wind=$xml->location->day[1]->hour[3]);
	echo $wind=$xml->location->day[1]->hour[3]->value;
	echo $wind=$xml->location->day[1]->hour[3]->temp[value];
	echo $wind=$xml->location->day[1]->hour[3]->temp[unit];
} elseif ($date >= 11 and $date < 14) {
    
	var_dump($wind=$xml->location->day[1]->hour[4]);
	
	print xml_attribute($wind=$xml->location->day[1]->hour[4], 'value');
	echo $wind=$xml->location->day[1]->hour[4]->value . "\n";
	echo $wind=$xml->location->day[1]->hour[4]->symbol[desc] . "\n";
	echo 'Температура воздуха: ' . $wind=$xml->location->day[1]->hour[4]->temp[value] . ' ' . $wind=$xml->location->day[1]->hour[4]->temp[unit] . "\n";
	echo 'Ощущается как: ' . $wind=$xml->location->day[1]->hour[4]->windchill[value] . ' ' . $wind=$xml->location->day[1]->hour[4]->windchill[unit];
	echo "\n";
	$ms = $wind=$xml->location->day[1]->hour[4]->wind[value];
	echo 'Ветер: ' . round($ms*1000/3600) . ' м/с';
	
} elseif ($date >= 14 and $date < 17) {
    var_dump($wind=$xml->location->day[1]->hour[5]);
} elseif ($date >= 17 and $date < 20) {
    var_dump($wind=$xml->location->day[1]->hour[6]);
} elseif ($date >= 20 and $date < 23) {
    var_dump($wind=$xml->location->day[1]->hour[7]);
}
	
	//var_dump($wind=$xml->location->day[1]->hour[1]);
    $humidity=$xml->location->day[0]->humidity[value];
    $tempy=$xml->location->day[0]->tempmin[value];
    $tempz=$xml->location->day[0]->tempmax[value];
    $temp = round(($tempy + $tempz) / 2);
    $pic=$xml->location->day[0]->symbol[value];
    $type=$xml->location->day[0]->symbol[desc];
    $speed=$xml->location->day[0]->wind[value];
    $speed = round($speed * 1000 / 3600);
    $pressure=$xml->location->day[0]->pressure[value];
    $pressure = round($pressure * 0.750064);
    //$humidity=$xml->fact->humidity;
    $t_2=$xml->location->day[0]->hour[1]->temp[value];
    if ($t_2>0) {$t_2='+'.$t_2;}
    $p_2=$xml->location->day[0]->hour[1]->symbol[value];
    $t_6=$xml->location->day[0]->hour[3]->temp[value];
    if ($t_6>0) {$t_6='+'.$t_6;}
    $p_6=$xml->location->day[0]->hour[3]->symbol[value];
    $t_10=$xml->location->day[0]->hour[5]->temp[value];
    if ($t_10>0) {$t_10='+'.$t_10;}
    $p_10=$xml->location->day[0]->hour[5]->symbol[value];
    $t_14=$xml->location->day[0]->hour[7]->temp[value];
    if ($t_14>0) {$t_14='+'.$t_14;}
    $p_14=$xml->location->day[0]->hour[7]->symbol[value];


    $tom_f_2=$xml->location->day[1]->tempmin[value];
    if ($tom_f_2>0) {$tom_f_2='+'.$tom_f_2;}
    $tom_f_6=$xml->location->day[2]->tempmin[value];
    if ($tom_f_6>0) {$tom_f_6='+'.$tom_f_6;}
    $tom_f_10=$xml->location->day[3]->tempmin[value];
    if ($tom_f_10>0) {$tom_f_10='+'.$tom_f_10;}
    $tom_f_14=$xml->location->day[4]->tempmin[value];
    if ($tom_f_14>0) {$tom_f_14='+'.$tom_f_14;}

    $tom_n_2=$xml->location->day[1]->tempmax[value];
    if ($tom_n_2>0) {$tom_n_2='+'.$tom_n_2;}
    $tom_n_6=$xml->location->day[2]->tempmax[value];
    if ($tom_n_6>0) {$tom_n_6='+'.$tom_n_6;}
    $tom_n_10=$xml->location->day[3]->tempmax[value];
    if ($tom_n_10>0) {$tom_n_10='+'.$tom_n_10;}
    $tom_n_14=$xml->location->day[4]->tempmax[value];
    if ($tom_n_14>0) {$tom_n_14='+'.$tom_n_14;}

    $type_y2=$xml->location->day[0]->hour[1]->symbol[desc];
    $type_y6=$xml->location->day[0]->hour[3]->symbol[desc];
    $type_y10=$xml->location->day[0]->hour[5]->symbol[desc];
    $type_y14=$xml->location->day[0]->hour[7]->symbol[desc];


    $type_2=$xml->location->day[1]->symbol[desc];
    $type_6=$xml->location->day[2]->symbol[desc];
    $type_10=$xml->location->day[3]->symbol[desc];
    $type_14=$xml->location->day[4]->symbol[desc];


    // Если значение температуры положительно, для наглядности добавляем "+"
    //$tomorrow = array("tomorrow");
    if ($temp>0) {$temp='+'.$temp;}
//    if ($tom_t_2>0) {$tom_t_2='+'.$tom_t_2;}
//    if ($tom_f_6>0) {$tom_f_6='+'.$tom_f_6;}
//    if ($tom_t_10>0) {$tom_t_10='+'.$tom_t_10;}
//    if ($tom_f_14>0) {$tom_f_14='+'.$tom_f_14;}
//   if ($tom_t_18>0) {$tom_t_18='+'.$tom_t_18;}
//    if ($tom_f_22>0) {$tom_f_22='+'.$tom_f_22;}
    $pic_0=$xml->location->day[1]->symbol[value];;
    $pic_1=$xml->location->day[2]->symbol[value];;
    $pic_2=$xml->location->day[3]->symbol[value];;
    $pic_3=$xml->location->day[4]->symbol[value];;

//--------------------------------------------------
    $patterns = array();
    $patterns[0] = '/NW/';
    $patterns[1] = '/NE/';
    $patterns[2] = '/SE/';
    $patterns[3] = '/SW/';
    $patterns[4] = '/N/';
    $patterns[5] = '/E/';
    $patterns[6] = '/S/';
    $patterns[7] = '/W/';
    $patterns[8] = '/CALM/';
    $replacements = array();
    $replacements[0] = 'Северо-западный';
    $replacements[1] = 'Северо-восточный';
    $replacements[2] = 'Юго-восточный';
    $replacements[3] = 'Юго-западный';
    $replacements[4] = 'Северный';
    $replacements[5] = 'Восточный';
    $replacements[6] = 'Южный';
    $replacements[7] = 'Западный';
    $replacements[8] = 'Штиль';
    $rest =  substr($time, -8);
    $rest2 = substr($time, -11, -9); 
    $rest1 = substr($time, -14, -12);
    $rest3 = substr($time, 0, 4);
//--------------------------------------------------------
$week = array(
    1 => 'Понедельник', 2 => 'Вторник', 3 => 'Среда', 4 => 'Четверг',
    5 => 'Пятница', 6 => 'Суббота', 7 => 'Воскресенье');
//echo($week[(date('w'))] . "\n");
//--------------------------------------------------------
$d = strtotime("+1 day");
$date1 =  date("d.m", $d); 
$d = strtotime("+2 day");
$date2 =  date("d.m", $d); 
$d = strtotime("+3 day");
$date3 =  date("d.m", $d); 
$d = strtotime("+4 day");
$date4 =  date("d.m", $d); 
$d = strtotime("+5 day");
$date5 =  date("d.m", $d); 
$d = strtotime("+6 day");
$date6 =  date("d.m", $d); 

//----------------------------------------------

//-----------------------------------------------

?>
