As you can see this is very long and repeats alot i need some help changing it in to a function.
The code basically does alot of calculatios with time.
<?php
//distance between town1 and town2
$Distance1 = sqrt((($North1 - $North2)*($North1 - $North2)) + (($East1 - $East2)*($East1 - $East2)));
//distance between town2 and town3
$Distance2 = sqrt((($North2 - $North3)*($North2 - $North3)) + (($East2 - $East3)*($East2 - $East3)));
//works out total distance for whole journey
$totaldistance = $Distance1 + $Distance2;
print "<p>Distance between " . $town1 . " and " . $town2 . " is " .round ( $Distance1, 2) . " Km";
print "<p>Distance between " . $town2 . " and " . $town3 . " is " .round ( $Distance2, 2) . " Km";
print "<p>Total distance to be travelled is " .round($totaldistance, 2) . " Km/h";
//Works out how long it will take from town1-town2 in hours
$time1 = $Dis1 / $Speed;
//split up this figure (time1) into minutes and seconds
$hours1 = (int)$time1;
$mins1 = (int)(($time1 - $hours1) * 60);
$secs1 = (int)(($time1 - $hours1) * 3600 - $mins1 * 60);
//Works out how long it will take from town2-town3 in hours
$time2 = $Dis2 / $Speed;
//split this figure (time2) into minutes and seconds
$hours2 = (int)$time2;
$mins2 = (int)(($time2 - $hours2) * 60);
$secs2 = (int)(($time2 - $hours2) * 3600 - $mins2 * 60);
//Works out total time of Journey
$totaltime = $time1 + $time2;
//split this figure (totaltime) into minutes and seconds
$totalhours = (int)$totaltime;
$totalmins = (int)(($totaltime - $totalhours) * 60);
$totalsecs = (int)(($totaltime - $totalhours) * 3600 - $totalmins * 60);
//set initial day and time settings
$day = 1;
$currenthour = 9;
$currentminute = 0;
$currentsecond = 0;
print "<p>Start time is 09:00:00";
print "<p>Depart from " . $town1 . " at 09:00:00 on Day " . $day . ".";
//set the second and minute variable for the current time
$currentsecond = $currentsecond + $secs1;
$currentminute = $currentminute + $mins1;
//add the hours to the total time
for ($h1=1; $h1<=$hours1; $h1++)
{
$currenthour = $currenthour + 1;
if ($currenthour == 18)
{
$currenthour = 9;
$day = $day + 1;
}
}
if ($currenthour < 10)
{
$currenthour = 9;
}
?>
<?
print "It will take " .round ($time1, 2) . " hours to get from " . $town1 . " to " . $town2 . " travelling at " . $speed . " km/h";
print "<p>Arrive at " . $town2 . " at ";
//print out time and day
if ($currenthour < 10)
{
print "0" . $currenthour . ":";
if ($currentminute < 10)
{
print "0" . $currentminute . ":";
if ($currentsecond < 10)
{
print "0" . $currentsecond;
}
else
{
print $currentsecond;
}
}
else
{
print $currentminute . ":";
if ($currentsecond < 10)
{
print "0" . $currentsecond;
}
else
{
print $currentsecond;
}
}
print " on Day " . $day . ".";
}
else
{
print $currenthour . ":";
if ($currentminute < 10)
{
print "0" . $currentminute . ":";
if ($currentsecond < 10)
{
print "0" . $currentsecond;
}
else
{
print $currentsecond;
}
}
else
{
print $currentminute . ":";
if ($currentsecond < 10)
{
print "0" . $currentsecond;
}
else
{
print $currentsecond;
}
}
print " on Day " . $day . ".";
}
?>
<?
print "Stay in " . $town2 . " for " . $stay1 . " mins.";
//add the minutes stayed to the current time variables
for ($h1=1; $h1<=$stay1; $h1++)
{
$currentminute = $currentminute + 1;
if ($currentminute == 60)
{
$currentminute = 0;
if ($currenthour == 18)
{
$currenthour = 9;
$day = $day + 1;
}
$currenthour = $currenthour + 1;
}
}
print "<p>Depart from " . $town2 . " at ";
//print out time and day
if ($currenthour < 10)
{
print "0" . $currenthour . ":";
if ($currentminute < 10)
{
print "0" . $currentminute . ":";
if ($currentsecond < 10)
{
print "0" . $currentsecond;
}
else
{
print $currentsecond;
}
}
else
{
print $currentminute . ":";
if ($currentsecond < 10)
{
print "0" . $currentsecond;
}
else
{
print $currentsecond;
}
}
print " on Day " . $day . ".";
}
else
{
print $currenthour . ":";
if ($currentminute < 10)
{
print "0" . $currentminute . ":";
if ($currentsecond < 10)
{
print "0" . $currentsecond;
}
else
{
print $currentsecond;
}
}
else
{
print $currentminute . ":";
if ($currentsecond < 10)
{
print "0" . $currentsecond;
}
else
{
print $currentsecond;
}
}
print " on Day " . $day . ".";
}
?>
<?
print "It will take " .round ($time2, 2) . " hours to get from " . $town2 . " to " . $town3 . " travelling at " . $speed . " km/h";
//change the font back to normal size
?>
<font size ="3">
<?
//add seconds to time, town2-town3
for($s1=1; $s1<=$secs2; $s1++)
{
$currentsecond = $currentsecond + 1;
if ($currentsecond ==60)
{
$currentsecond = 0;
$currentminute = $currentminute + 1;
}
}
//add minutes ti time, town2-town3
for($m1=1; $m1<=$mins2; $m1++)
{
$currentminute = $currentminute + 1;
if ($currentminute ==60)
{
$currentminute = 0;
$currenthour = $currenthour + 1;
}
}
//add hours to time, town2-town3
for ($h1=1; $h1<=$hours2; $h1++)
{
$currenthour = $currenthour + 1;
if ($currenthour == 18)
{
$currenthour = 9;
$day = $day + 1;
}
}
print "<p>Arrive at " . $town3 . " at ";
//print out time and day
if ($currenthour < 10)
{
print "0" . $currenthour . ":";
if ($currentminute < 10)
{
print "0" . $currentminute . ":";
if ($currentsecond < 10)
{
print "0" . $currentsecond;
}
else
{
print $currentsecond;
}
}
else
{
print $currentminute . ":";
if ($currentsecond < 10)
{
print "0" . $currentsecond;
}
else
{
print $currentsecond;
}
}
print " on Day " . $day . ".";
}
else
{
print $currenthour . ":";
if ($currentminute < 10)
{
print "0" . $currentminute . ":";
if ($currentsecond < 10)
{
print "0" . $currentsecond;
}
else
{
print $currentsecond;
}
}
else
{
print $currentminute . ":";
if ($currentsecond < 10)
{
print "0" . $currentsecond;
}
else
{
print $currentsecond;
}
}
print " on Day " . $day . ".";
}
?>