Added some helpers for date string formatting.
This commit is contained in:
+39
-9
@@ -4,10 +4,10 @@
|
||||
* Returns true with a given percentage.
|
||||
*
|
||||
* @param int $prob Probability for true in percent.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function rand_bool($prob = 50) {
|
||||
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.');
|
||||
@@ -28,11 +28,40 @@ function rand_bool($prob = 50) {
|
||||
* argument.
|
||||
*
|
||||
* @param integer $decimals Sets the number of decimal points.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function formatAmount(float $amount, $decimals = 2) {
|
||||
return number_format($amount, $decimals, ',', '.');
|
||||
function formatAmount(float $amount, $cash = true, $text = false,
|
||||
$decimals = 2)
|
||||
{
|
||||
$euro = ($cash) ? ' €' : '';
|
||||
if (!$text) {
|
||||
$sign = ($amount < 0) ? '- ' : '';
|
||||
|
||||
$left = '<span style=\'float: left;\'>';
|
||||
$right = '<span style=\'float: right;\'>';
|
||||
$span = '</span>';
|
||||
|
||||
if ($amount == 0) {
|
||||
return $right . '- ' . $euro . $span;
|
||||
}
|
||||
|
||||
return $left . $sign . $span . $right .
|
||||
number_format(abs($amount), $decimals, ',', '.') . $euro . $span;
|
||||
}
|
||||
|
||||
return number_format(abs($amount), $decimals, ',', '.') . $euro;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a formatted date string.
|
||||
*
|
||||
* @param string $date
|
||||
* @param string $format
|
||||
* @return string
|
||||
*/
|
||||
function formatDate($date, $format='d.m.Y')
|
||||
{
|
||||
return Carbon\Carbon::parse($date)->format($format);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,9 +72,10 @@ function formatAmount(float $amount, $decimals = 2) {
|
||||
* @param string $month
|
||||
* @return string
|
||||
*/
|
||||
function startOfMonth($year, $month) {
|
||||
function startOfMonth($year, $month, $format='d.m.Y')
|
||||
{
|
||||
return Carbon\Carbon::createFromDate($year, $month)
|
||||
->startOfMonth()->format('Y-m-d');
|
||||
->startOfMonth()->format($format);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,7 +86,7 @@ function startOfMonth($year, $month) {
|
||||
* @param string $month
|
||||
* @return string
|
||||
*/
|
||||
function endOfMonth($year, $month) {
|
||||
function endOfMonth($year, $month, $format='d.m.Y') {
|
||||
return Carbon\Carbon::createFromDate($year, $month)
|
||||
->endOfMonth()->format('Y-m-d');
|
||||
->endOfMonth()->format($format);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user