1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
<?php
namespace App;
use Silex\Application;
class Util
{
public static function numberFormat(Application $app, $number, $decimals = 2)
{
$last_locale = setlocale(LC_ALL, 0);
if (isset($app['locale'])) {
setlocale(LC_ALL, $app['locale']);
}
$locale = localeconv();
$locale_id = strtok(setlocale(LC_NUMERIC, 0), '_.;');
switch ($locale_id) {
case 'ru':
$locale['thousands_sep'] = ' ';
break;
}
$formatted = number_format(
$number,
$decimals,
$locale['decimal_point'],
$locale['thousands_sep']
);
switch ($locale_id) {
case 'ru':
$tmp = explode($locale['decimal_point'], $formatted);
if (5 == strlen($tmp[0])) {
$tmp[0] = strtr($tmp[0], [$locale['thousands_sep'] => '']);
$formatted = implode($locale['decimal_point'], $tmp);
}
break;
}
setlocale(LC_ALL, $last_locale);
return $formatted;
}
public static function isRemainingExecutionTimeBiggerThan($miliseconds = 5000)
{
$max_execution_time = ini_get('max_execution_time');
if ($max_execution_time === 0) {
return true;
}
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$spendMiliseconds = (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) * 1000;
} else {
$resourceUsages = getrusage();
$spendMiliseconds = $resourceUsages['ru_utime.tv_sec'] * 1000 + $resourceUsages['ru_utime.tv_usec'] / 1000;
}
$remainingMiliseconds = $max_execution_time * 1000 - $spendMiliseconds;
return ($remainingMiliseconds >= $miliseconds);
}
}