$item_count) { $last_item = $item_count; } if($page_count == 1) { $range_text = "Showing $item_count items"; } else { $range_text = "Showing $first_item - $last_item of $item_count items"; } return (object) [ "page" => $page, "pageCount" => $page_count, "pageSize" => $page_size, "offset" => $offset, "firstItem" => $first_item, "lastItem" => $last_item, "rangeText" => $range_text ]; } /** * @param string $folder * @param int $id * @return string */ public static function getItemDirectory(string $folder, int $id): string { $shard = str_pad($id % 1000, 3, "0", STR_PAD_LEFT); return ROOT_DIR . "/files/" . $folder . "/" . $shard . "/" . $id; } /** * @param string $folder * @param int $id * @return string */ public static function createItemDirectory(string $folder, int $id): string { $directory = Util::getItemDirectory($folder, $id); if(!file_exists($directory)) { mkdir($directory, 0777, true); } return $directory; } /** * @param string $string * @param string $glue * @return string */ public static function slug(string $string, string $glue = "-"): string { $slug = strtolower($string); $slug = preg_replace("/[^\da-z]/i", $glue, $slug); // Replace anything not 0-9 a-z with glue $slug = preg_replace("/" . preg_quote($glue, "/") . "+/", $glue, $slug); // Replace consequential glue with dash return trim($slug, $glue); } /** * @param string $string * @param int $id * @return string */ public static function slugWithId(string $string, int $id): string { return Util::slug($string) . "-" . $id; } /** * @param int $size * @return string */ public static function getDisplayFileSize(int $size): string { if($size < pow(2, 10)) return number_format($size) . " bytes"; if($size < pow(2, 20)) return number_format($size / pow(2, 10), 2) . " KB"; if($size < pow(2, 30)) return number_format($size / pow(2, 20), 2) . " MB"; if($size < pow(2, 40)) return number_format($size / pow(2, 30), 2) . " GB"; return number_format($size / pow(2, 40), 2) . " TB"; } /** * @param int $unix_time * @param array $options * @return string */ public static function timeDifference(int $unix_time, $options = []): string { $plain = $options["plain"] ?? false; $now = time(); $difference = $now - $unix_time; $tense = ($difference >= 0) ? "ago" : "to go"; $difference = abs($difference); if($difference < 60) { $value = $difference; $class = "text-danger"; $unit = "second"; } else if($difference < 3600) { $value = floor($difference / 60); $class = "text-danger"; $unit = "minute"; } else if($difference < 86400) { $value = floor($difference / 3600); $class = "text-warning"; $unit = "hour"; } else if($difference < 2592000) { $value = floor($difference / 86400); $class = ""; $unit = "day"; } else if($difference < 31536000) { $value = floor($difference / 2592000); $class = "text-muted"; $unit = "month"; } else { $value = floor($difference / 31536000); $class = "text-muted"; $unit = "year"; } $text = "$value $unit" . ($value != 1 ? "s" : "") . " $tense"; if($plain) { return "$text"; } else { return "$text"; } } /** * @return array * @throws Exception */ public static function getTimeZones(): array { // Taken and modified from: https://stackoverflow.com/a/17355238 static $regions = [ DateTimeZone::AFRICA, DateTimeZone::AMERICA, DateTimeZone::ANTARCTICA, DateTimeZone::ASIA, DateTimeZone::ATLANTIC, DateTimeZone::AUSTRALIA, DateTimeZone::EUROPE, DateTimeZone::INDIAN, DateTimeZone::PACIFIC, ]; $identifiers = []; foreach($regions as $region) { $identifiers = array_merge($identifiers, DateTimeZone::listIdentifiers($region)); } $time_zone_offsets = []; foreach($identifiers as $identifier) { $tz = new DateTimeZone($identifier); $time_zone_offsets[] = (object) [ "identifier" => $identifier, "offset" => $tz->getOffset(new DateTime), ]; } uasort($time_zone_offsets, function($a, $b) { if($a->offset > $b->offset) return 1; else if($a->offset < $b->offset) return -1; else return strcasecmp($a->identifier, $b->identifier); }); $sr = [ "/" => ", ", "St" => "St.", "_" => " ", ]; $time_zone_list = []; foreach($time_zone_offsets as $object) { $identifier = $object->identifier; $offset = $object->offset; $display_identifier = str_replace(array_keys($sr), array_values($sr), $identifier); $offset_prefix = $offset < 0 ? "-" : "+"; $offset_formatted = gmdate("H:i", abs($offset)); $pretty_offset = "UTC${offset_prefix}${offset_formatted}"; $time_zone_list[$identifier] = "${pretty_offset} - $display_identifier"; } return $time_zone_list; } /** * @param bool $time * @return string */ public static function getDate($time = false): string { if(!$time) $time = time(); return gmdate("Y-m-d", $time); } /** * @param bool $time * @return string */ public static function getUTCDateTime($time = false): string { return Util::getDateTime($time); } /** * @param bool $time * @return string */ public static function getDateTime($time = false): string { if(!$time) $time = time(); return gmdate("Y-m-d H:i:s", $time); } /** * @param string $dts * @return false|int */ public static function getUnixTimeFromUTC(string $dts) { return strtotime($dts . " UTC"); } /** * @param string $dts1 * @param string $dts2 * @return float|int */ public static function getDurationFromDTS(string $dts1, string $dts2) { $tz = date_default_timezone_get(); date_default_timezone_set("UTC"); $t1 = strtotime($dts1); $t2 = strtotime($dts2); date_default_timezone_set($tz); return abs($t2 - $t1); } /** * @param int|bool $time * @return string */ public static function getDisplayDateTime($time = false): string { if($time === false) $time = time(); $current_year = date("Y"); $year = date("Y", $time); $format = ($year == $current_year) ? "M j, H:i" : "M j Y, H:i"; return date($format, $time); } /** * @param int|bool $time * @return string */ public static function getDisplayDate($time = false): string { if($time === false) $time = time(); $current_year = date("Y"); $year = date("Y", $time); $format = ($year == $current_year) ? "M j" : "M j Y"; return date($format, $time); } /** * @param string $dts * @return string */ public static function getDisplayDateTimeFromUTC(string $dts): string { $unix = Util::getUnixTimeFromUTC($dts); return Util::getDisplayDateTime($unix); } /** * @param array $array * @param string $field * @param string $compare_function * @return array */ public static function sortObjectArray(array $array, string $field, string $compare_function = ""): array { usort($array, function($a, $b) use ($compare_function, $field) { switch($compare_function) { default: if($a->$field == $b->$field) return 0; return ($a->$field < $b->$field) ? -1 : 1; case "strnatcasecmp": return strnatcasecmp($a->$field, $b->$field); } }); return $array; } /** * @param array $input * @return array */ public static function arrayFlipByID(array $input = []): array { $output = []; foreach($input as $object) { $output[$object->getID()] = $object; } return $output; } public static function getDateTimeFromUtcString(?string $dts): ?CarbonImmutable { return $dts ? CarbonImmutable::parse($dts, "UTC") : null; } public static function clamp(int $value, int $min, int $max): int { if($value < $min) { return $min; } if($value > $max) { return $max; } return $value; } }