PHP code example of teamchallengeapps / distance

1. Go to this page and download the library: Download teamchallengeapps/distance library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

teamchallengeapps / distance example snippets


'providers' => [
    'TeamChallengeApps\Distance\DistanceServiceProvider'
];


use TeamChallengeApps\Distance\Distance;

$meters = new Distance(100, 'meters');
$km = new Distance(10.5, 'kilometers');
$miles = new Distance(10, 'miles');
$steps = new Distance(10000, 'footsteps');



$meters = new Distance(100);



$meters = new Distance(1000);

$km = $meters->toKilometers();

echo $km->value; // 1



$meters = new Distance(1000);

echo $meters->asUnit('kilometers'); // 1
echo $meters->value; // 1000



$meters = new Distance(1000.995);

echo $meters->value; // 1000.995
echo $meters->round(); // 1001.00



$distance new Distance(0);

if ($distance->isEmpty()) {
  //
}

if ($distance->isZero()) {
  
}



$distance = new Distance(10);
$total = new Distance(100);

if ($distance->lt($total)) {
  // Less than
}

if ($distance->lte($total)) {
  // Less than or equal
}

if ($distance->gt($total)) {
  // Greater than
}

if ($distance->gte($total)) {
  // Greater than or equal
}



$distance = new Distance(10);
$total = new Distance(100);

$percentage = $distance->percentageOf($total); // 10



$distance = new Distance(150);
$total = new Distance(100);

$percentage = $distance->percentageOf($total); // 100
$percentage = $distance->percentageOf($total, false); // 150



$total = new Distance(1000);
$logged = new Distance(10);

$total->increment($logged); 

echo $total->value; // 1010



$total = new Distance(1010);
$redeemed = new Distance(10);

$total->decrement($logged); 

echo $total->value; // 1000



$distance = new Distance(100500.591);

echo $distance; // 10,500.59

$value = (string) $distance;

echo $value; // 10,500.59



return [

    'format' => [

        'comma' => true,
        'suffix' => false,

    ];

];



$meters = new Distance(100, 'meters');
echo $meters->toStringWithSuffix(); // 1000 m

$km = new Distance(10.5, 'kilometers');
echo $km->toStringWithSuffix(); // 1000 km

$miles = new Distance(10, 'miles');
echo $miles->toStringWithSuffix(); // 1000 mi.

$steps = new Distance(10000, 'footsteps');
echo $steps->toStringWithSuffix(); // 1000 steps


php artisan vendor:publish --provider="TeamChallengeApps\Distance\DistanceServiceProvider" --tag="config"