Distance between postcodes code in full - But how can I have just 1 decimal place in distance?

StackOverflow https://stackoverflow.com/questions/19526691

  •  01-07-2022
  •  | 
  •  

Domanda

I have an amazing piece of code that works out the distance between 2 postcodes in both; distance (miles), and driving time.

The trouble now being - I am getting the distance in a very long form (eg: 10.2454014 miles) I just want it to just have 1 decimal place (eg 10.2 miles).

Also, if it's not too difficult - when running the above code as is, you will notice that driving time is 80 minutes. Well that's just silly. Does anyone know how to make it say the time in hours if over 60 minutes, and minutes if under 60?

Amazing if anyone can help - This code is really useful for people!

<?php
        function get_driving_information($start, $finish, $raw = false)
{
    if(strcmp($start, $finish) == 0)
    {
        $time = 0;
        if($raw)
        {
            $time .= ' seconds';
        }

        return array('distance' => 0, 'time' => $time);
    }

    $start  = urlencode($start);
    $finish = urlencode($finish);

    $distance   = 'unknown';
    $time       = 'unknown';

    $url = 'http://maps.googleapis.com/maps/api/directions/xml?origin='.$start.'&destination='.$finish.'&sensor=false';
    if($data = file_get_contents($url))
    {
        $xml = new SimpleXMLElement($data);

        if(isset($xml->route->leg->duration->value) AND (int)$xml->route->leg->duration->value > 0)
        {
            if($raw)
            {
                $distance = (string)$xml->route->leg->distance->text;
                $time     = (string)$xml->route->leg->duration->text;
            }
            else
            {
                $distance = (int)$xml->route->leg->distance->value / 1000 / 1.609344;
                $time     = (int)$xml->route->leg->duration->value/ 60;
            }
        }
        else
        {
            throw new Exception('Could not find that route');
        }

        return array('distance' => $distance, 'time' => $time);
    }
    else
    {
        throw new Exception('Could not resolve URL');
    }
}

try
{
    $info = get_driving_information('fy1 4bj', 'ls1 5ns');
    echo $info['distance'].' miles ' . 'That\'s about ' .$info['time'].' minutes drive from you';
}
catch(Exception $e)
{
    echo 'Caught exception: '.$e->getMessage()."\n";
}
?>
È stato utile?

Soluzione

Use number_format() to format decimal/float values to desired decimal points. Use gmdate() function to convert time (in seconds) to formatted value like: Hours:Mins:Sec

// Format Distance
$info['distance'] = number_format($info['distance'], 2);

// Format time
$info['time'] = gmdate('H:i:s', ($info['time'] * 60));

// Output
echo $info['distance'] .' miles ' . 'That\'s about ' .$info['time'].' minutes drive from you';

Altri suggerimenti

As far as rounding goes, just do this:

echo round($info['distance'], 1) . ' miles '

For your 80 minutes -> 1 hour 20 minutes, see this post: https://stackoverflow.com/a/8563576/2812842

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top