PDA

View Full Version : Ferry Timetable Script, "Ferry Leaves in ? mins"



Waldo Pepper
02-23-2011, 12:26 AM
Hi,

I'm not sure whether this would be better as PHP or Javascript, so thought i'd try here first.

Basically what I want is to display on a line in my website that "The next ferry leaves in XX mins". The website page is in PHP.

I have a ferry timetable, which leaves at varying intervals throughout the day. There are 2 timetables, one for Monday to Friday, the other for Saturday/Sunday.

Is this achievable?

I've seen it done in iPhone apps, in fact I have an app on my iPhone for this very ferry schedule.

I know a little bit of both PHP & Javascript, but only enough to get me into trouble and to think that it could be done in either of these.

Thanks for your help,
WP

vwphillips
02-23-2011, 01:48 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/
var Departures=[
['10:30','11:30','12:10','14:30','15:30','16:30','17:30','18:30','19:30','20:30'],
['10:30','11:30','12:10','14:30','15:30','16:30','17:30','18:30','19:30','20:30'],
['10:30','11:30','12:10','14:30','15:30','16:30','17:30','18:30','19:30','20:30'],
['10:30','11:30','12:10','14:30','15:30','16:30','17:30','18:30','19:30','20:30'],
['10:30','11:30','12:10','14:30','15:30','16:30','17:30','18:30','19:30','20:30'],
['10:30','12:10','14:30'],
['10:30','12:10','14:30']
]

function Departure(id,ary,os){
var today=new Date().getTime();
today=new Date(today+os);
var year=today.getFullYear();
var month=today.getMonth();
var date=today.getDate();
var ary=ary[today.getDay()];
var hm,d;
for (var z0=0;z0<ary.length;z0++){
hm=ary[z0].split(/\D/);
d=new Date(year,month,date,hm[0],hm[1]);
if (d>today){
date=d;
var minutes=parseInt((d-today)/1000/60);
var hours=parseInt(minutes/60);
minutes-=60*hours;
document.getElementById(id).innerHTML=((hours<10?'0':'')+hours)+':'+((minutes<10?'0':'')+minutes);
break;
}
}
setTimeout(function(){ Departure(id,ary,os); },60*1000);
}

var SeverDate='2011:1:23:14:34'; // the ferry port date and time - PHP used to produce this date

var sms=SeverDate.split(/\D/);
sms=new Date(sms[0],sms[1],sms[2],sms[3],sms[4]).getTime();
var ms=new Date().getTime();


/*]]>*/
</script>
</head>

<body onload="Departure('tst',Departures,sms-ms);">
<span id="tst" ></span>
</body>

</html>