From cc203f36c755fc147aee1fc96d6c6798c94fdc62 Mon Sep 17 00:00:00 2001 From: Lukas Halbritter Date: Mon, 1 Oct 2018 17:17:41 +0200 Subject: [PATCH] Added some helpers for date string formatting. --- app/Http/helpers.php | 56 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/app/Http/helpers.php b/app/Http/helpers.php index 87cf47b..ec4f914 100644 --- a/app/Http/helpers.php +++ b/app/Http/helpers.php @@ -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,35 +28,65 @@ 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 = ''; + $right = ''; + $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); } /** * Return the string that is the first day of the month, given by the year and * the month. * - * @param string $year - * @param string $month + * @param string $year + * @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); } /** * Return the string that is the last day of the month, given by the year and * the month. * - * @param string $year - * @param string $month + * @param string $year + * @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); }