63 lines
1.6 KiB
PHP
63 lines
1.6 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Returns true with a given percentage.
|
|
*
|
|
* @param int $prob Probability for true in percent.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
function rand_bool($prob = 50) {
|
|
if ($prob < 0 or $prob > 100) {
|
|
throw new InvalidArgumentException('The value for the probability '.
|
|
'must be in a range from 0 to 100.');
|
|
} else if (!is_int($prob)) {
|
|
throw new InvalidArgumentException('rand_bool only accepts integers.');
|
|
}
|
|
|
|
$rand = rand(1, 100);
|
|
|
|
return $rand <= $prob;
|
|
}
|
|
|
|
/**
|
|
* Return the formatted string representation of a floatng point number.
|
|
*
|
|
* The decimal point is a ',', the thousands seperator ist set to '.'.
|
|
* The number of decimals is 2 by default but can be set by the $decimals
|
|
* argument.
|
|
*
|
|
* @param integer $decimals Sets the number of decimal points.
|
|
*
|
|
* @return string
|
|
*/
|
|
function formatAmount(float $amount, $decimals = 2) {
|
|
return number_format($amount, $decimals, ',', '.');
|
|
}
|
|
|
|
/**
|
|
* Return the string that is the first day of the month, given by the year and
|
|
* the month.
|
|
*
|
|
* @param string $year
|
|
* @param string $month
|
|
* @return string
|
|
*/
|
|
function startOfMonth($year, $month) {
|
|
return Carbon\Carbon::createFromDate($year, $month)
|
|
->startOfMonth()->format('Y-m-d');
|
|
}
|
|
|
|
/**
|
|
* Return the string that is the last day of the month, given by the year and
|
|
* the month.
|
|
*
|
|
* @param string $year
|
|
* @param string $month
|
|
* @return string
|
|
*/
|
|
function endOfMonth($year, $month) {
|
|
return Carbon\Carbon::createFromDate($year, $month)
|
|
->endOfMonth()->format('Y-m-d');
|
|
}
|