decimalPoint === null) { $locale = localeconv(); $this->decimalPoint = $locale['decimal_point']; $this->thousandsSeparator = $locale['thousands_sep']; } return number_format($value, $precision, $this->decimalPoint, $this->thousandsSeparator); } /** * Returns a prettified time value (in seconds). * * @param int $numberOfSeconds The number of seconds. * @param bool $displayTimeAsSentence If set to true, will output `"5min 17s"`, if false `"00:05:17"`. * @param bool $round Whether to round to the nearest second or not. * @return string */ public function getPrettyTimeFromSeconds($numberOfSeconds, $displayTimeAsSentence = false, $round = false) { $numberOfSeconds = $round ? (int)$numberOfSeconds : (float)$numberOfSeconds; $isNegative = false; if ($numberOfSeconds < 0) { $numberOfSeconds = -1 * $numberOfSeconds; $isNegative = true; } // Display 01:45:17 time format if ($displayTimeAsSentence === false) { $hours = floor($numberOfSeconds / 3600); $minutes = floor(($reminder = ($numberOfSeconds - $hours * 3600)) / 60); $seconds = floor($reminder - $minutes * 60); $time = sprintf("%02s", $hours) . ':' . sprintf("%02s", $minutes) . ':' . sprintf("%02s", $seconds); $centiSeconds = ($numberOfSeconds * 100) % 100; if ($centiSeconds) { $time .= '.' . sprintf("%02s", $centiSeconds); } if ($isNegative) { $time = '-' . $time; } return $time; } $secondsInYear = 86400 * 365.25; $years = floor($numberOfSeconds / $secondsInYear); $minusYears = $numberOfSeconds - $years * $secondsInYear; $days = floor($minusYears / 86400); $minusDays = $numberOfSeconds - $days * 86400; $hours = floor($minusDays / 3600); $minusDaysAndHours = $minusDays - $hours * 3600; $minutes = floor($minusDaysAndHours / 60); $seconds = $minusDaysAndHours - $minutes * 60; $precision = ($seconds > 0 && $seconds < 0.01 ? 3 : 2); $seconds = round($seconds, $precision); if ($years > 0) { $return = sprintf(Piwik::translate('General_YearsDays'), $years, $days); } elseif ($days > 0) { $return = sprintf(Piwik::translate('General_DaysHours'), $days, $hours); } elseif ($hours > 0) { $return = sprintf(Piwik::translate('General_HoursMinutes'), $hours, $minutes); } elseif ($minutes > 0) { $return = sprintf(Piwik::translate('General_MinutesSeconds'), $minutes, $seconds); } else { $return = sprintf(Piwik::translate('General_Seconds'), $seconds); } if ($isNegative) { $return = '-' . $return; } return $return; } /** * Returns a prettified memory size value. * * @param number $size The size in bytes. * @param string $unit The specific unit to use, if any. If null, the unit is determined by $size. * @param int $precision The precision to use when rounding. * @return string eg, `'128 M'` or `'256 K'`. */ public function getPrettySizeFromBytes($size, $unit = null, $precision = 1) { if ($size == 0) { return '0 M'; } $units = array('B', 'K', 'M', 'G', 'T'); $currentUnit = null; foreach ($units as $idx => $currentUnit) { if ($size >= 1024 && $unit != $currentUnit && $idx != count($units) - 1) { $size = $size / 1024; } else { break; } } return round($size, $precision) . " " . $currentUnit; } /** * Returns a pretty formated monetary value using the currency associated with a site. * * @param int|string $value The monetary value to format. * @param int $idSite The ID of the site whose currency will be used. * @return string */ public function getPrettyMoney($value, $idSite) { $space = ' '; $currencySymbol = self::getCurrencySymbol($idSite); $currencyBefore = $currencySymbol . $space; $currencyAfter = ''; // (maybe more currencies prefer this notation?) $currencySymbolToAppend = array('€', 'kr', 'zł'); // manually put the currency symbol after the amount if (in_array($currencySymbol, $currencySymbolToAppend)) { $currencyAfter = $space . $currencySymbol; $currencyBefore = ''; } // if the input is a number (it could be a string or INPUT form), // and if this number is not an int, we round to precision 2 if (is_numeric($value)) { if ($value == round($value)) { // 0.0 => 0 $value = round($value); } else { $precision = GoalManager::REVENUE_PRECISION; $value = sprintf("%01." . $precision . "f", $value); } } $prettyMoney = $currencyBefore . $value . $currencyAfter; return $prettyMoney; } /** * Returns a percent string from a quotient value. Forces the use of a '.' * decimal place. * * @param float $value * @return string */ public function getPrettyPercentFromQuotient($value) { $result = ($value * 100) . '%'; return Common::forceDotAsSeparatorForDecimalPoint($result); } /** * Returns the currency symbol for a site. * * @param int $idSite The ID of the site to return the currency symbol for. * @return string eg, `'$'`. */ public static function getCurrencySymbol($idSite) { $symbols = self::getCurrencyList(); $currency = Site::getCurrencyFor($idSite); if (isset($symbols[$currency])) { return $symbols[$currency][0]; } return ''; } /** * Returns the list of all known currency symbols. * * @return array An array mapping currency codes to their respective currency symbols * and a description, eg, `array('USD' => array('$', 'US dollar'))`. */ public static function getCurrencyList() { static $currenciesList = null; if (is_null($currenciesList)) { require_once PIWIK_INCLUDE_PATH . '/core/DataFiles/Currencies.php'; $currenciesList = $GLOBALS['Piwik_CurrencyList']; } return $currenciesList; } }