PHP output error

The following code execute correct every day but is giving me an error, just the LAST DAY OF THE MONTH. Since the output of the last day of the month looks for day 32 (instead of 1st of November) in order to work correctly.
Here is the incorrect output
http://mesowest.utah.edu/cgi-bin/droman/meso_download_mesowest.cgi?product=&stn=TIST&unit=0&time=GMT&day1=32&month1=10&year1=2012&hour1=0&hours=24&output=csv

Here is the PHP code
[php]

<?php require_once("/var/www/app/elmer/connections/connection_index.php"); $query = sprintf("SELECT `id`,`code` FROM `stations` WHERE platform = 'mesowest'");//echo $query.'
'; $result = mysql_query($query); $now_datetime = gmdate("Y-m-d H:00:00"); $val = explode(' ',$now_datetime); $date_string = explode('-',$val[0]); $date_string[2]+=1; while($info = mysql_fetch_assoc($result)){ $query = sprintf("SELECT MAX(date) FROM `mesowest` WHERE station_id = '".$info['id']."'");//echo $query.'
'; $result2 = mysql_query($query); $info2 = mysql_fetch_assoc($result2); $query = sprintf("SELECT MAX(time) FROM `mesowest` WHERE date = '".$info2['MAX(date)']."' AND station_id = '".$info['id']."'");//echo $query.'
'; $result3 = mysql_query($query); $info3 = mysql_fetch_assoc($result3); $maxTime=split(':',$info3['MAX(time)']); $maxDate=split('-',$info2['MAX(date)']); $timemax = gmmktime($maxTime[0],$maxTime[1],"00",$maxDate[1],$maxDate[2],$maxDate[0])-(10*3600); echo "http://mesowest.utah.edu/cgi-bin/droman/meso_download_mesowest.cgi?product=&stn=".$info['code']."&unit=0&time=GMT&day1=$date_string[2]&month1=$date_string[1]&year1=$date_string[0]&hour1=0&hours=24&output=csv
"; $file = fopen("http://mesowest.utah.edu/cgi-bin/droman/meso_download_mesowest.cgi?product=&stn=".$info['code']."&unit=0&time=GMT&day1=$date_string[2]&month1=$date_string[1]&year1=$date_string[0]&hour1=0&hours=24&output=csv", "r") or exit("Unable to open file!"); $time=split(':',$info3['MAX(time)']); $control = 0; while(!feof($file)){ if($control >= 20 && $control <= 43){ $control++; $values = explode(',',fgets($file)); $j=0; foreach($values as $val){ //if(empty($val)){$val="empty";} //echo $j." -> ".$val."
"; if(!empty($val)){$realvalues[]=$val;} $j++; } /*$j=0; foreach($realvalues as $val){ if(empty($val)){$val="empty";} echo $j." -> ".$val."
"; $j++; }die();*/ if($realvalues[1] < 10){$realvalues[1]='0'.$realvalues[1];} if($realvalues[0] < 10){$realvalues[0]='0'.$realvalues[0];} $date = $realvalues[2]."-".$realvalues[0]."-".$realvalues[1]; $time = $realvalues[3].":".$realvalues[4].":00"; $wdir = str_replace('MM','-999',$realvalues[11]); $spd = str_replace('MM','-999',$realvalues[9])*0.86898; $gust = str_replace('MM','-999',$realvalues[10])*0.86898; $atem = str_replace('MM','-999',$realvalues[6]); if($atem != '-999'){$atem = (5/9)*($atem-32);} //echo $realvalues[3].",".$realvalues[4].","."00".",".$realvalues[1].",".$realvalues[0].",".$realvalues[2].'
'; $timeval = gmmktime($realvalues[3],$realvalues[4],"00",$realvalues[0],$realvalues[1],$realvalues[2]); //echo $timeval." <= ".$timemax.'
'; if($timeval <= $timemax){unset($realvalues);continue;} //echo $realvalues[3].",".$realvalues[4].","."00".",".$realvalues[1].",".$realvalues[2].",".$realvalues[0].'
'; //echo $info2['MAX(date)']." == ".$date." && ".$maxTime[0]."==".$realvalues[3]." && ".$maxTime[1]."==".$realvalues[4]."
"; if($info2['MAX(date)']<$date){ $query = sprintf("INSERT INTO `mesowest` (`station_id`,`date`,`time`,`Wind Direction (-4m)`,`Wind Speed (-4m)`,`Air Temperature (-3m)`,`Wind Gust (-4m)`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s')",$info['id'], $date, $time, $wdir, $spd, $atem, $gust);echo $query.'
';//die(); mysql_query($query); unset($realvalues); continue; } $time2=split(':',$pieces[0]); if($info2['MAX(date)']<=$date && $maxTime[0]<$realvalues[3]){ $query = sprintf("INSERT INTO `mesowest` (`station_id`,`date`,`time`,`Wind Direction (-4m)`,`Wind Speed (-4m)`,`Air Temperature (-3m)`,`Wind Gust (-4m)`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s')",$info['id'], $date, $time, $wdir, $spd, $atem, $gust);echo $query.'
';//die(); mysql_query($query); unset($realvalues); continue; } unset($realvalues); } else{ fgets($file); $control++; } } fclose($file); } mysql_close($connection); exit(0); ?>

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service