Skip to content

Instantly share code, notes, and snippets.

@jkoop
jkoop / numberToMetric.php
Last active August 12, 2021 21:35 — forked from liunian/gist:9338301
php8.0 - number to metric with prefix for things like human readable file size
<?php
function numberToMetric(int|float $number, int $significantDigits = 3, bool $binary = false): string {
static $prefixes = ['y','z','a','f','p','n','μ','m','','k','M','G','T','P','E','Z','Y'];
$factor = floor((strlen(abs($number)) - 1) / 3) * (abs($number) <=> 1);
$metricBase = $binary ? 1024 : 1000;
$number = $number / pow($metricBase, $factor);
return sprintf("%.{$significantDigits}H", $number) . $prefixes[$factor+8];
@jkoop
jkoop / ClosureTable.php
Last active September 20, 2021 20:04 — forked from mohanklein/ClosureTable.php
Closure Table Trait for Laravel Eloquent Models
<?php // app/Http/Traits/ClosureTable.php
namespace App\Models\Traits;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Facades\Db;
/**
@jkoop
jkoop / readable-dates.php
Last active March 15, 2024 15:34 — forked from scr4bble/readable-dates.php
Adminer plugin that replaces UNIX timestamps with human-readable dates.
<?php
/** This plugin replaces UNIX timestamps with human-readable dates in your local timezone.
* Mouse click on the date field reveals timestamp back.
*
* @link https://www.adminer.org/plugins/#use
* @author Anonymous
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
*/