Code: Select all
<?php
include_once('../simple_html_dom.php');
class Time{
function times($departure, $destination) {
$today = date("Y-m-d");
$dateToday=$this->next_business_day($today);
$tok = strtok($dateToday, "-");
$contDate=0;
$f=true;
while ($tok !== false&&$f=true) {
if($contDate==0){
$year=$tok;
$year=substr( $year, 1 );
$year=substr( $year, 1 );
}
if($contDate==1){
$month=$tok;
}
if($contDate==2){
$day=$tok;
$f=false;
}
$contDate=$contDate+1;
$tok = strtok("-");
}
$url='http://198.175.56.231/shop/airsearch?type=air&ar.type=oneWay&ar.ow.leaveSlice.orig.key='.$departure.'&ar.ow.leaveSlice.dest.key='.$destination.'&ar.ow.leaveSlice.date='.$day.'%2F'.$month.'%2F'.$year.'&ar.ow.leaveSlice.time=Anytime&ar.ow.numAdult=1&ar.ow.numSenior=0&ar.ow.numChild=0&ar.ow.child%5B0%5D=&ar.ow.child%5B1%5D=&ar.ow.child%5B2%5D=&ar.ow.child%5B3%5D=&ar.ow.child%5B4%5D=&ar.ow.child%5B5%5D=&ar.ow.child%5B6%5D=&ar.ow.child%5B7%5D=&_ar.ow.nonStop=0&_ar.ow.narrowSel=0&ar.ow.narrow=airlines&ar.ow.carriers%5B0%5D=&ar.ow.carriers%5B1%5D=&ar.ow.carriers%5B2%5D=&ar.ow.cabin=C&search=Search+Flights';
$html = file_get_html($url);
$my_array1 = array ();
$i=0;
$pattern = "/Leave/";
foreach($html->find('span') as $element) {
$my_array1[$i]=$element;
$i=$i+1;
}
$k=0;
$my_array2 = array ();
$pattern = "/Leave/";
$pattern1 = "/stop/";
$cont=0;
while ( $k < sizeof($my_array1)) {
$element1=$my_array1[$k];
$element1 = str_replace("<span class='sliceLabel'>","",$element1);
$element1 = str_replace("<span class='duration'>","",$element1);
$element1 = str_replace("</span>","",$element1);
$element1 = str_replace(" ","",$element1);
$element1 = str_replace(" ","",$element1);
$element1=trim($element1);
if (preg_match($pattern, $element1)) {
$z=$k+8;
if(preg_match($pattern1, $my_array1[$z])){
$j=$k+9;
$my_array2[$cont]=$my_array1[$j];
$cont=$cont+1;
}
}
$k=$k+1;
}
$m=0;
$cont2=0;
$N=sizeof($my_array2);
$my_array3 = array ();
while ($m < $N) {
$my_array2[$m]=str_replace("hr",":",$my_array2[$m]);
$my_array2[$m]=str_replace("min",":",$my_array2[$m]);
$variable=$my_array2[$m];
$cont=0;
$tok = strtok($variable, ":");
$f=true;
while ($tok !== false&&$f==true) {
if($cont==0){
$h=$tok;
$h=trim($h);
$h = preg_replace("/[^A-Za-z0-9]/", "", $h);
$h=str_replace("spanclassduration","",$h);
$hour=(int)$h;
}
if($cont==1){
$mi=$tok;
$mi=trim($mi);
$minute=(int)$mi;
$time = 60*($hour)+$minute;
$res = $time / 60.0;
$f=false;
}
$cont=$cont+1;
$tok = strtok(":");
}
$my_array3[$m]=$res;
++$m;
}
$avg1=$this->Average($my_array3);
$minorTime1=$this->MinorTime($my_array3);
return array($avg1, $minorTime1);
}
function Average($my_array3) {
$mk=0;
$total=0;
$lenArr3= sizeof($my_array3);
while ( $mk <$lenArr3) {
$total=$total+$my_array3[$mk];
$mk=$mk+1;
}
$aveg=$total/$lenArr3;
$aveg=$this->round_two_decimals($aveg);
return $aveg;
}
function MinorTime($my_array3) {
$lenArr3= sizeof($my_array3);
$minorTime=$my_array3[0];
$pos=0;
for($fcont=1;$fcont<$lenArr3;$fcont++) {
if ($my_array3[$fcont]<$minorTime) {
$minorTime=$my_array3[$fcont];
$pos=$fcont;
}
}
$minorTime=$this->round_two_decimals($minorTime);
return $minorTime;
}
function round_two_decimals($value) {
$float_round=round($value * 100) / 100;
return $float_round;
}
function next_business_day($date) {
$add_day = 1;
do {
$add_day++;
$new_date = date('Y-m-d', strtotime("$date +$add_day Days"));
$new_day_of_week = date('w', strtotime($new_date));
} while($new_day_of_week == 6 || $new_day_of_week == 0);
return $new_date;
}
?>
Code: Select all
<?php
include_once('Time.php');
$obj = new Time;
$tmp = $obj->times('LAX', 'FRA');
$averg = $tmp[0];
$minor_T = $tmp[1]
echo "Avg Travel Time=".$averg."\n";
echo "Fastest Time=".$minor_T."\n";*
$obj1 = new Time;
$tmp1 = $obj1->times('LAX', 'PAR');
$averg1 = $tmp1[0];
$minor_T1 = $tmp1[1];
echo "Avg Travel Time=".$aver1."\n";
echo "Fastest Time=".$minor_T1."\n";*/
?>