PHP code example of softproidea / php-helpers

1. Go to this page and download the library: Download softproidea/php-helpers 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/ */

    

softproidea / php-helpers example snippets


use Softproidea\PhpHelpers\{Ids, Dates, TimeRange, Geo, Pricing, Phone, Str};

Ids::uuid();                 // "3f2b8c1e-9a47-4d6b-8e0f-2c5a7d9b1e43"
Ids::compactUuid();          // "3f2b8c1e9a474d6b8e0f2c5a7d9b1e43"
Ids::token(8);               // "9f86d081884c7d65"
Ids::otp();                  // "048213"
Ids::reference('inv');       // "48213INV"
Ids::sequence(42);           // "000042"
Ids::millis();               // 1796211378663

Dates::isValid('2026-02-30');                              // false
Dates::format('2026-12-02', 'd M Y');                      // "02 Dec 2026"
Dates::utcToLocal('2026-12-02 06:00:00', 'Asia/Colombo');  // "2026-12-02 11:30:00"
Dates::localToUtc('2026-12-02 11:30:00', 'Asia/Colombo');  // "2026-12-02 06:00:00"
Dates::convertTimezone('2026-12-02 09:00:00', 'Europe/London', 'Asia/Tokyo'); // "2026-12-02 18:00:00"
Dates::isWeekend('2026-12-05');                            // true
Dates::isWithinRange('2026-12-01', '2026-12-31', '2026-12-31'); // true
Dates::datesBetween('2026-12-01', '2026-12-15', 'monday'); // ["2026-12-07", "2026-12-14"]
Dates::shiftMonth(2026, 12, 1);                            // ['year' => 2027, 'month' => 1, 'monthName' => 'January']
Dates::startOfMonth('2026-12-02');                         // "2026-12-01"
Dates::endOfMonth('2026-12-02');                           // "2026-12-31"
Dates::age('1996-01-01', '2026-06-01');                    // 30
Dates::monthLabel('2026-03-10');                           // "March-2026"
Dates::monthLabel('2026-03-10', 'Commission-From');        // "Commission-From-March-2026"
Dates::nextWeekday(['Monday', 'Thursday'], '2026-12-02');  // "2026-12-03"

Dates::monthDays(2026, 12, '2026-12-03')[2];
// [
//   'date' => '2026-12-03', 'day' => 3, 'dayName' => 'Thu', 'dayLetter' => 'T',
//   'isToday' => false, 'isSelected' => true, 'isWeekend' => false,
// ]

TimeRange::slots('09:00', '12:00');
// [['start' => '09:00', 'end' => '10:00'], ['start' => '10:00', 'end' => '11:00'], ['start' => '11:00', 'end' => '12:00']]
TimeRange::slots('09:00', '10:30', 30);   // three 30-minute slots

TimeRange::hourlyLabels(8, 9);
// [['key' => '08:00 AM', 'value' => '08:00 AM'], ['key' => '09:00 AM', 'value' => '09:00 AM']]

TimeRange::addHours('23:00', 2);                       // "01:00"
TimeRange::addMinutes('23:50', 20);                    // "00:10"
TimeRange::overlaps('10:00', '12:00', '11:00', '13:00'); // true
TimeRange::overlaps('10:00', '12:00', '12:00', '13:00'); // false
TimeRange::overlaps('22:00', '02:00', '01:00', '03:00'); // true (crosses midnight)
TimeRange::contains('09:00', '17:00', '10:00', '12:00'); // true
TimeRange::minutesBetween('22:00', '02:00');           // 240
TimeRange::isWithin('23:30', '22:00', '02:00');        // true
TimeRange::to12Hour('00:00');                          // "12:00 AM"
TimeRange::to24Hour('02:30 PM');                       // "14:30"
TimeRange::toMinutes('01:15');                         // 75

Geo::isValidCoordinate('6.9', '79.8');          // true
Geo::isValidCoordinate(91, 0);                  // false
Geo::parse('6.9271, 79.8612');                  // [6.9271, 79.8612]
Geo::parse('hello');                            // null

Geo::distanceMetres(6.9271, 79.8612, 6.9344, 79.8428); // ~2187.2
Geo::distanceKm(0, 0, 1, 0);                    // ~111.19
Geo::isWithinRadius(6.9275, 79.8615, 6.9271, 79.8612, 100); // true

$fence = [
    ['lat' => 6.9, 'lng' => 79.8],
    ['lat' => 6.9, 'lng' => 79.9],
    ['lat' => 7.0, 'lng' => 79.9],
];
Geo::isInsidePolygon(6.95, 79.88, $fence);              // true
Geo::isInsidePolygon(6.95, 79.88, json_encode($fence)); // true

Geo::bearing(0, 0, 0, 1);                       // 90.0 (due east)
Geo::destination(0, 0, 90, 1000);               // ['lat' => 0.0, 'lng' => ~0.00899]
Geo::boundingBox($fence);                       // ['minLat' => 6.9, 'minLng' => 79.8, 'maxLat' => 7.0, 'maxLng' => 79.9]
Geo::centroid($fence);                          // ['lat' => ~6.9333, 'lng' => ~79.8667]
Geo::pathLengthMetres([[0, 0], [1, 0], [2, 0]]); // ~222390.0

$slabs = [
    ['from' => 5,  'to' => 10, 'rate' => 80],
    ['from' => 10, 'to' => 20, 'rate' => 60],
];

Pricing::tiered(3,  5, 500, $slabs, 50);   // 500.0   (within base)
Pricing::tiered(8,  5, 500, $slabs, 50);   // 740.0   (500 + 3 x 80)
Pricing::tiered(15, 5, 500, $slabs, 50);   // 1200.0  (500 + 5 x 80 + 5 x 60)
Pricing::tiered(25, 5, 500, $slabs, 50);   // 1750.0  (... + 5 x 50 extra)

Pricing::applyDiscount(1000, 15);   // ['original' => 1000.0, 'discount' => 150.0, 'final' => 850.0]
Pricing::addPercent(100, 8);        // ['base' => 100.0, 'charge' => 8.0, 'total' => 108.0]
Pricing::percentOf(200, 12.5);      // 25.0
Pricing::split(1000, 20);           // ['share' => 200.0, 'remainder' => 800.0]
Pricing::divide(10.00, 3);          // [3.34, 3.33, 3.33]
Pricing::parse('1,234.50');         // 1234.5
Pricing::parse('abc');              // null
Pricing::format(1234.5);            // "1,234.50"
Pricing::format(1234.5, 2, ',', '.'); // "1.234,50"
Pricing::round(2.456);              // 2.46

Phone::toE164('077 123 4567', '94');   // "+94771234567"
Phone::toE164('+94 77 123 4567');      // "+94771234567"
Phone::toE164('0094771234567');        // "+94771234567"
Phone::toE164('94771234567', '+94');   // "+94771234567"
Phone::toE164('abc');                  // null
Phone::isValidE164('+94771234567');    // true
Phone::stripPrefix('tel:94771234567'); // "94771234567"
Phone::mask('+94771234567');           // "+9477****567"

Str::firstName('  Jane  Q. Doe ');          // "Jane"
Str::slug('Hello, World!');                 // "hello-world"
Str::truncate('Hello world', 6);            // "Hello…"
Str::maskEmail('[email protected]');     // "j******[email protected]"
Str::isJson('{"a":1}');                     // true
Str::parseList("['mon', 'wed']");           // ["mon", "wed"]
Str::parseList('a,b');                      // ["a", "b"]
Str::parseList('[]');                       // []
Str::inListIgnoreCase('Unnamed Road', ['unnamed road', 'un-named road']); // true
Str::startsWithAny('tel:94', ['sip:', 'tel:']); // true

use Softproidea\PhpHelpers\Excel\{Text, SheetReader};
use PhpOffice\PhpSpreadsheet\IOFactory;

Text::title('net_profit-x');            // "Net profit x"
Text::formatAmount('1234567.500');      // "1,234,567.5"
Text::parseNumber('1,234');             // 1234
Text::hasDatePlaceholder('As at <DD/MM>'); // true
Text::isTicked('✓');                    // true

$sheet = IOFactory::load('report.xlsx')->getSheetByName('Balance Sheet');
$reader = new SheetReader($sheet, ['dateFormat' => 'Y-m-d', 'precision' => 5, 'strict' => false]);

$col = $reader->findHeaderColumn('net profit');       // 1
$row = $reader->findSectionStart('Assets', 3);        // 10
$reader->value($row + 1, $col);                       // "99"
$reader->valueBelowLabel(10, 'Total', 4);             // value under the "Total" label
bash
composer